鑑於我有兩個foreach
:第一個輸出所有類別,第二個,應該輸出目前類別的每個品牌。如何實現這樣的請求到數據庫
例子:
// Controller
$categories = DB::table('products')
->join('categories', 'products.c_id', '=', 'categories.id')
->select('categories.name as cat')
->groupBy('categories.name')
->get();
$brands = DB::table('products')
->join('categories', 'products.c_id', '=', 'categories.id')
->join('brands', 'products.b_id', '=', 'brands.id')
->select('brands.name as brand')
->where('categories.id', '=', '1')<-- the problem part
->groupBy('brands.name')
->get();
//view
@foreach($categories as $category)
<li><a>{{ $category->cat}}</a>
<ul class="sort-section">
<li><a href="#"><strong>All {{$category->cat}}</strong></a></li>
@foreach($brands as $brand)
<li><a href="#">{{ $brand->brand }}</a></li>
@endforeach
</ul>
</li>
@endforeach
我不知道如何正確地輸出。現在我得到的記錄,where categories.id = 1
(我做它來檢查我的代碼是否可以工作),而不是1
我應該使用id
的當前category
。換句話說,如果我想讓它發生,無論如何,我會在我的視圖中執行第一個foreach
中的第二個查詢(這很瘋狂)。
有什麼建議嗎?
只是所以我明白了這個問題,你想顯示的品牌列表只是與選擇相關的品牌編輯類別? – Birdman
@Birdman是的,這對我來說是最難的部分:在第一個'foreach'期間,我獲得了類別的值。而對於艾維品類,我必須得到不同的品牌。這是第二個查詢中的「where」部分 – newbie