Php 拉雷维尔

Php 拉雷维尔,php,laravel,eloquent,Php,Laravel,Eloquent,你好 我想做这样的事情: $ids = [1, 2, 3]; $posts = $user->posts()->whereIn('id', $ids)->get(); 但是我得到一个内部服务器错误。有没有其他方法可以做到这一点 在本例中,假设ID不是主键,并且多篇文章可以具有相同的ID。因此: $ids = [1, 2, 3]; $posts = Post::whereIn('id', $ids)->get(); 不会返回所需的结果。出现错误的原因是mysql无法使用

你好

我想做这样的事情:

$ids = [1, 2, 3];
$posts = $user->posts()->whereIn('id', $ids)->get();
但是我得到一个内部服务器错误。有没有其他方法可以做到这一点

在本例中,假设ID不是主键,并且多篇文章可以具有相同的ID。因此:

$ids = [1, 2, 3];
$posts = Post::whereIn('id', $ids)->get();

不会返回所需的结果。

出现错误的原因是
mysql
无法使用
id
列查看
表中的内容。在
where()中使用
tableName

$posts = $user->posts()->whereIn('posts.id', $ids)->get();
N.B:我假设您的
Post
表名为
posts
请使用like

  $items = DB::table('items')->whereIn('id', [1, 2, 3])->get();
链接:


还可以使用laravel 5.4

您得到的实际错误是什么?假设您打算编写
其中('id',$id)
为什么要将arr作为参数而不是$id传递?您也可以尝试这样做
$posts=Post::where('id',arr)->toSql()
并检查查询是否正在成功生成。有人能解释一下为什么在这个答案中进行向下投票吗?这很有帮助。非常感谢。我不是投反对票的那个人。您出现错误的原因是mysql无法使用id查看您所引用的表column@LukhoMdingi看看我更新的答案,它会让你对你的问题有一个清晰的认识。我没有试过这个@Sohel0415的答案奏效了。谢谢你的贡献。
  User::select('id','name','email','phone','created_at')->whereIN('id',[1,2,3,4,5,6,7,8,9,9,10,11])->where('is_guest',NULL)->where('user_type','1')->orderBy('id','desc')->take(20)->get();