Я работаю на странице поиска, и ниже указан код, который я использую.
public function index(Request $request){
$data = $request->input();
// dd($data);
$category_id = '';
$products = Product::select('products.*','b.name as brand_name')
->leftjoin('brands as b','b.id','products.brand_id')
->with('product_image')
->whereHas('product_sizes.product_size')
->whereHas('product_color')
->where('status','A')
->whereNull('b.deleted_at')
->whereNull('products.deleted_at');
//->get()
//->toArray();
//echo "<pre>";print_r($products);die;
$sub_category_id = '';
if(!empty(@$data['sub_category_id'])){
$sub_category_id = $data['sub_category_id'];
$products = $products->where('products.sub_category_id',$data['sub_category_id']);
$category_id = SubCategory::where('id',$data['sub_category_id'])
->whereNull('deleted_at')
->value('category_id');
}
$sub_sub_category_id = '';
if(!empty(@$data['sub_sub_category_id'])){
$sub_sub_category_id = $data['sub_sub_category_id'];
$products = $products->where('products.sub_sub_category_id',$data['sub_sub_category_id']);
$sub_category_id = SubSubCategory::where('id',$data['sub_sub_category_id'])
->whereNull('deleted_at')
->value('sub_category_id');
$category_id = SubCategory::where('id',$sub_category_id)
->whereNull('deleted_at')
->value('category_id');
/*$category_id = SubCategory::where('id',$data['sub_sub_category_id'])
->whereNull('deleted_at')
->value('category_id');*/
}
$brand_id = '';
if(!empty(@$data['brand_id'])){
$brand_id = $data['brand_id'];
$products = $products->where('brand_id',$data['brand_id']);
$category_id = Brand::where('id',$data['brand_id'])
->whereNull('deleted_at')
->value('category_id');
}
if(@$_GET['category_id']){
$category_id = $_GET['category_id'];
}
if(!empty($category_id)){
$products = $products->where('products.category_id',$category_id);
}
if (!empty(@$data['search'])){
$search = trim($data['search']);
// echo $search; die;
/*$products = $products->where('products.title','like','%'.$search.'%')
->orWhere('products.description','like','%'.$search.'%')
->orWhere('b.name','like','%'.$search.'%');*/
$products = $products->where(function($q) use($search){
$q->where('products.title','like','%'.$search.'%')
->orWhere('products.description','like','%'.$search.'%')
->orWhere('b.name','like','%'.$search.'%');
});
// echo "<pre>";print_r($products->get()->toArray());die;
$categories = Category::with('sub_categories','sub_categories.sub_sub_categories')
->whereNull('deleted_at')
->get()
->toArray();
$brands = Brand::select()
->whereNull('deleted_at')
->get()
->toArray();
}else{
$sub_categories = SubCategory::with('sub_sub_categories')
->where('category_id',$category_id)
->whereNull('deleted_at')
->get()
->toArray();
$brands = Brand::where('category_id',$category_id)
->whereNull('deleted_at')
->get()
->toArray();
}
$products = $products->get()->toArray();
// dd($products);
// dd($categories);
if(Auth::check()) {
$user_id = Auth::id();
$wishlists = Wishlist::select()
->where('user_id',$user_id)
->pluck('product_id')->toArray();
}
// echo "<pre>";print_r($products['description'][0]['id']);die;
$filters = Filter::select('*')
->whereNull('deleted_at')
->orderBy('from_value','asc')
->get()
->toArray();
// dd($search);
return view('frontEnd.products.index',compact('products','categories','sub_categories','brands','category_id','wishlists','sub_category_id','brand_id','search','filters','sub_sub_category_id'));
}
Я хочу получать продукты в соответствии с categories
и subcategories
при выборе на первой странице. Ниже приведено изображение главной страницы. Men wears
- это подкатегория, а shirts
- это подкатегория. Я получаю пустые продукты прямо сейчас.
Первая страница
Ниже приведена таблица продуктов: -
-----------------------------------------------------------------------------------
| id | category_id | sub_category_id | sub_sub_category_id | title |
-----------------------------------------------------------------------------------
| 1 | 1 | 1 | 1 | abc |
-----------------------------------------------------------------------------------
Ниже приведена таблица категорий : -
--------------------
| id | name |
--------------------
| 1 | men |
--------------------
Ниже приведена таблица sub_category: -
--------------------------------------
| id | category_id | name |
--------------------------------------
| 1 | 1 | men wears |
--------------------------------------
Ниже приведена таблица sub_sub_category: -
-----------------------------------------------------------
| id | category_id | sub_category_id | name |
-----------------------------------------------------------
| 1 | 1 | 1 | shirts |
-----------------------------------------------------------