![](/img/trans.png)
[英]How to use orderby on element that was joined with Laravel Eloquent method WITH
[英]Laravel 5: How to use 'where' or 'orderBy' using eloquent?
首先,這不是Laravel 4的重復:如何使用Eloquent ORM“排序”
我有這個模型用戶,然后我有這個其他模型的想法。 我正在編寫一個包含用戶詳細信息的視圖,我想發布用戶建議的所有想法。 現在,這些想法都有一個狀態,第一個狀態是“DRAFT”,這意味着只有發布該想法的用戶才能查看它,這個想法不應該顯示給查看該配置文件的其他用戶。
使用雄辯,我可以做類似的事情:
@foreach($user->idea as $idea)
...display details...
@endforeach
我的問題是,這種方法會循環所有的想法,並按照它們的介紹順序循環。 我可以添加一個@if
來顯示我想要的東西,比如
@foreach($user->idea as $idea)
@if($idea->status !='DRAFT')
...show the idea...
@endif
@endforeach
但我不認為這是明智的做法,我想做點什么
$ideas = $user->idea->where('status', '<>', 'DRAFT')
然后循環通過$ideas
變量,或者至少,我喜歡像somtehing一樣
$ideas = $user->idea->orderBy('created_at', 'desc')
但我不知道該怎么做。
你可以這樣做:
$ideas = $user->idea()
->where('status', '<>', 'DRAFT')
->orderBy('created_at', 'desc')
->get();
當你使用->idea()
,它將啟動一個查詢。
有關如何查詢關系的更多信息: https : //laravel.com/docs/5.7/eloquent-relationships#querying-relations
您可以使用where where子句
DB::table('products_to_categories')
->where('categories_id', $id )
->update([
'categories_id' => $unassigned_cat_id
]);
你可以像這樣使用orderBy子句
DB::table('products_to_categories')
->where('categories_id', $id )
->orderBy('subId','DESC');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.