Php 如何使用Eloquent获取最后9条记录,但排除具有特定ID的记录?
我正在尝试从用户处获取9张最新的图像,但是,我还希望排除id为$id的图像。我当前拥有的图像将获取最后9张图像,其中可能包括id为Php 如何使用Eloquent获取最后9条记录,但排除具有特定ID的记录?,php,laravel,eloquent,Php,Laravel,Eloquent,我正在尝试从用户处获取9张最新的图像,但是,我还希望排除id为$id的图像。我当前拥有的图像将获取最后9张图像,其中可能包括id为id的$id的图像。我不想在结果中包含id为$id的图像 public function specificImage($id){ $image = Image::find($id); $authorId = $image->user_id; $recentImages = Image::where('parent_id', NULL)-&
id
的$id
的图像。我不想在结果中包含id
为$id
的图像
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();
}
这应该行吗
$recentImages = Image::where('parent_id', NULL)->where('user_id', $authorId)->whereNotIn('id', [$id])->orderBy('created_at', 'desc')->limit(9)->get();
这应该行吗
$recentImages = Image::where('parent_id', NULL)->where('user_id', $authorId)->whereNotIn('id', [$id])->orderBy('created_at', 'desc')->limit(9)->get();
只需使用
WHERE
子句:
Image::where('id', '!=', $id)->/* ... */->get()
只需使用
WHERE
子句:
Image::where('id', '!=', $id)->/* ... */->get()
遗憾的是,查询仍然返回id为$id.Lol的图像,没有注意到您的答案。@AshfaqAhmed,来自帮助中心:“您可以接受您认为是解决问题的最佳解决方案的答案。”遗憾的是,查询仍然返回id为$id.Lol的图像,没有注意到您的答案。@AshfaqAhmed,来自帮助中心:“您可以接受您认为是解决您的问题的最佳解决方案的答案。”请看,我可以问where('id',!=',$id)和whereNotIn('id',[$id])之间有什么区别吗?它们都很好用。
whereNotIn
接受一个值数组来检查给定的列。whereNotIn接受一个数组,因此您可以向但是既然你只需要一个id,那么只需where!=
更合适。我可以问一下where('id',!=',$id)和whereNotIn('id',[$id])之间有什么区别吗?它们都很好用。whereNotIn
接受一个值数组来检查给定的列。whereNotIn接受一个数组,所以你可以给“但是由于您只需要一个id,所以只需where!=
更合适