Onyx
Onyx

Reputation: 5782

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();
}

Upvotes: 0

Views: 145

Answers (2)

Elias Soares
Elias Soares

Reputation: 10254

Just use a WHERE clause:

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

Upvotes: 4

Ashfaque Ali Solangi
Ashfaque Ali Solangi

Reputation: 1891

This should work ?

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

Upvotes: 1

Related Questions