简体   繁体   中英

How to get last 9 records but exclude the record with a certain ID using Eloquent?

I am trying to get the 9 most recent images from a user, however, I also would like to exclude the image with an id of $id. What I currently have gets the last 9 images, which might include the image with id of $id . I would like to somehow not include the image with id of $id in the result.

public function specificImage($id){
    $image = Image::find($id);
    $authorId = $image->user_id;
    $recentImages = Image::where('parent_id', NULL)->where('user_id', $authorId)->orderBy('created_at', 'desc')->limit(9)->get();
}

只需使用WHERE子句:

Image::where('id', '!=', $id)->/* ... */->get()

这应该工作吗?

$recentImages = Image::where('parent_id', NULL)->where('user_id', $authorId)->whereNotIn('id', [$id])->orderBy('created_at', 'desc')->limit(9)->get();

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