在我的情况下,我有渠道表是在多对多关系类别表。哪里查询whereaas不工作,因为它应该在Laravel 5.4
我想获取他们的类别包含动态值的渠道,比如对LIKE
查询每个类别的标题。但我需要通过Where子句检查频道的另一列。
这是一个通道结构:
[
{
"id": 87,
"username": "ch1",
"title": "ch title",
"type": "channel",
"members": 210424,
"location": "tabriz",
"created_at": "2017-05-09 01:39:44",
"updated_at": "2017-05-16 17:19:28",
}
]
得到我想要什么,我运行此查询:
$channels = Channel::orderBy('members', 'desc')
->whereHas('categories', function ($query) use ($category) {
$query->where('title', 'LIKE', '%' . $category . '%');
})
->where([
['visible', 1],
['title', 'LIKE', '%' . $trend . '%'],
['location', 'LIKE', '%' . $location . '%'],
['members', '>', $members]])
->get();
上查询返回我所有的渠道像查询,而whereHas
当我单独使用'whereHas'时,它会返回频道与我想要的类别!
我使用错误的语法吗?并排使用where
和whereHas
子句是否属实?
UPDATE:
我需要做渠道的标题或用户名like
查询,所以我用orWhere
但结果却是像以前一样!它会将所有频道返回给我!像查询没有whereHas
再次。 所以我用这个查询做到这一点,
$channels = Channel::orderBy('members', 'desc')
->whereHas('categories', function ($query) use ($category) {
$query->where('title', 'LIKE', '%' . $category . '%');
})
->where([
['visible', 1],
['title', 'LIKE', '%' . $trend . '%'],
['location', 'LIKE', '%' . $location . '%'],
['members', '>', $members]])
->orWhere([
['visible', 1],
['username', 'LIKE', '%' . $trend . '%'],
['location', 'LIKE', '%' . $location . '%'],
['members', '>', $members]])
->get();
起初我以为这个问题是排序依据子句@exprator赛义德,但加入后orWhere
我了解,orderBy
不是我的问题。
你在哪里应用条件分类? –