我正在使用 laravel 5.2
开发一个项目。但是现在我遇到了一个问题,在我的数据库中,有两个表users
和comments
。 users
表包含 id、email、username、password、created_at、updated_at
列。 comments
表包含id, user_id, content, created_at, updated_at, deleted_at
。现在在我的代码中,我有一个包含一些评论 ID 的 array
。我想要获得拥有这些评论的 users
。所以这是我的代码:
$commentIds = [
....
...
...
];
$users = User::whereHas('comments',function($query)use($commentIds){
$query->whereIn('id',$commentIds);
});
但是我得到异常说列 id 不明确。
最佳答案
$users = User::whereHas('comments',function($query)use($commentIds){
$query->whereIn('comments.id',$commentIds);
});
会起作用
关于Laravel 5.2 whereHas 列不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45656686/