在我的情況下,我有渠道表是在多對多關係類別表。哪裏查詢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
不是我的問題。
你在哪裏應用條件分類? –