简体   繁体   中英

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. So if a model other than $rowId exists, you may proceed on deleted $rowId model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM