我有3個表:
homepage
訂單- 整數boolean[/span]。
product_category_id - integer[/span]。
類別
name- string[/span]。
slug- 鼻涕蟲
parent_id- integer[/span
產品
name- string 命名。
category_id- 整數
我想在主頁上有15個類別的產品,包括隨機排序的子類別的產品。到目前為止,我所做的就是
//所有類別都要在主頁上發布。
$hp_categories = HomepageCategory::asc()->active()-> get()。
foreach ($hp_categories as $hp_cat) {
//陣列宣告
$cat_with_subcat_arr = [];
//類別
$product_category = ProductCategory::where('id', $hp_cat-> product_category_id)-> first();
//push category's id to array。
array_push($cat_with_subcat_arr, $product_category->id) 。
// subcategories if any.
$product_category->childrenCategoriesIds($cat_with_subcat_arr)。
//通過whereIn從類別和子類別中得到產品。
$products = Product:: whereIN('product_category_id', $cat_with_subcat_arr)->inRandomOrder()-> limit(15)-> get();
dd($products)。
}
return $hp_categories;
注意:我不能通過急于加載來做到這一點。所以,我通過簡單的邏輯來做,但無法將資料傳遞給視圖。
uj5u.com熱心網友回復:
在控制器中我做了
/define a collection;
$hp_cats = collect();
foreach ($hp_categories as $key=> $hp_cat) {
//陣列宣告 //陣列宣告
$cat_with_subcat_arr = [];
//類別
$product_category = ProductCategory::where('id', $hp_cat-> product_category_id)-> first();
//push category's id to array。
array_push($cat_with_subcat_arr, $product_category->id) 。
// subcategories if any.
$product_category->childrenCategoriesIds($cat_with_subcat_arr)。
//通過whereIn從類別和子類別中得到產品。
$products = Product::whereIN('product_category_id', $cat_with_subcat_arr)
->inRandomOrder()
->limit(15)
->get();
$hp_cats[$key] = $products。
}
注意:我無法找到答案,所以它可能不是最好的解決方案。任何幫助都是值得贊賞的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/320620.html
標籤:
上一篇:如何在資料透視表上應用過濾器?



