Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 是否可以在一个方法中使用多个关系约束?_Laravel_Eloquent - Fatal编程技术网

Laravel 是否可以在一个方法中使用多个关系约束?

Laravel 是否可以在一个方法中使用多个关系约束?,laravel,eloquent,Laravel,Eloquent,我有一个如下的查询: Post::whereHas('comments', function ($query) { $query->where('content', 'like', 'foo%'); })->whereHas('comments', function ($query) { $query->where('content', 'like', 'bar%'); })->get() 像一个需要所有帖子的人一样,这些帖子有一条评论,文本像“foo”

我有一个如下的查询:

Post::whereHas('comments', function ($query) {
    $query->where('content', 'like', 'foo%');
})->whereHas('comments', function ($query) {
    $query->where('content', 'like', 'bar%');
})->get()

像一个需要所有帖子的人一样,这些帖子有一条评论,文本像“foo”,还有一条评论,文本像“bar”。我可以把这两个请求混合在一个请求中吗

是的,可以链接多个where子句,但只能使用
一次(在模型名称之后),对于所有其他子句,它应该是
->
,就像添加
->get()
一样

所以你最终会得到:

Post::whereHas('comments', function ($query) {
    $query->where('content', 'like', 'foo%');
})->whereHas('comments', function ($query) {
    $query->where('content', 'like', 'bar%');
})->get()

我只是打错了,我用的是箭头而不是双分号。我想问的是,有没有办法不使用“whereHas”两次?是的,第二个
whereHas
应该命名为
或whereHas