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
。