加入的foreach laravel查询
问题描述:
public function deals(){
$matchThese = [ 'suspended' => 0, 'status' => 1, 'approved' => 1 ];
$deals = ListsDeals::where($matchThese)->orderBy('start_date')->get();
$list = DB::table('lists')
->join('list_has_deals', 'lists.id', '=', 'list_has_deals.list_id')
->where('list_has_deals.deal_id', '=', 12)
->select('lists.id')
->get();
$categories = DB::table('lists_categories')
->join('list_has_categories', 'lists_categories.id', '=', 'list_has_categories.category_id')
->where('list_has_categories.list_id', '=', $list[0]->id)
->select('lists_categories.title')
->get();
return view("deals")
->with("deals", $deals)
->with("categories", $categories);
}
我想获得冠军的名单范畴的价值,但首先我需要获取列表中的优惠少言
我有
Deals - Lists - lists_has_deals
Categories - Lists - lists_has_deals
都是belongsToMany,我想在交易中获得属于与报价相关联的列表的类别。
在我的例子中,工作正常,但12需要成为$交易的foreach不是数字静态,感谢您的帮助。
答
尝试使用list_has_deals上有许多通过或中间透视表。如果我理解你的问题,列表应该对交易和类别都有必要的参考。
https://laravel.com/docs/5.4/eloquent-relationships#has-many-through
https://laravel.com/docs/5.4/eloquent-relationships#many-to-many 具体withPivot()