[英]Laravel, how to check if this is the only record in a table
Is there any way to check if this is the only record in a table, so if yes then don't delete it, else delete it, what I have is just check if its the last one and this is not what Im looking for有什么方法可以检查这是否是表中唯一的记录,所以如果是,则不要删除它,否则删除它,我所拥有的只是检查它是否是最后一个,这不是我要找的
$order=EditUserorder::find($rowId);
$last_record = EditUserorder::orderBy('id', 'desc')->first();
if($order->id == $last_record->id)
I believe this would work我相信这会奏效
if(EditUserorder::count() == 1) {
#...
}
You could do this:你可以这样做:
if(EditUserorder::where('id', '!=', $rowId)->exists()) {
// other records exist
EditUserorder::find($rowId)->delete(); //delete the record
}
The exists method checks if any model was returned. exists 方法检查是否返回了任何模型。 So if a model other than $rowId exists, you may proceed on deleted $rowId model.因此,如果存在 $rowId 以外的模型,您可以继续处理已删除的 $rowId 模型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.