mercredi 6 juillet 2022

laravel inside with leftjoin is not working

I have added left join inside with but it is not working at all. Please suggest or guide.

$q = $q->leftJoin('product_sort_order', function ($join) {
 $join->on('products.id', '=', 'product_sort_order.product_id');
 $join->on('products.sub_category_id', '=', 'product_sort_order.type_id');
})->orderBy('product_sort_order.sort_order', 'ASC');

Controller code (trying to get category products with relationship)

$category = Category::query();
        $category->where('status', '=', '1');
        $category->where('id', $sub_category_id);
        // $category->where('status', '!=', '2');
        $category = $category->with([
            'subproducts' => function ($q) use ($request, $sub_category_id) {
                $q->select('products.*');
                $q->where('products.status', '=', 1);
                $q->where('products.is_pendding', '=', 0);
                $q->where('products.sub_category_id', $sub_category_id);
                if ($request->get('theme')) {
                    $q->Where('products.theme_id', 'LIKE', '%' . $request->get('theme') . '%');
                }
                if ($request->get('categories')) {
                    $q->where('products.category_id', getCategoryIdFromSlug($request->get('categories')));
                }
                if ($request->get('sections')) {
                    $q->where('products.section_category_id', getSectionIdFromSlug($request->get('sections')));
                }
                if ((App::getLocale()) == 'en') {
                //    $q->orderBy('product_name_lang->en');
                } else {
                //    $q->orderBy('product_name_lang->fr');
                }
                if ($request->get_j0_product == true) {
                    $q->where('products.delivery', 'J0');
                }
                // $q->orderBy('sale_price', 'DESC');
                // $q->orderBy('created_at', 'DESC');

                // $q = $q->leftJoin('product_sort_order', function ($join) {
                //         $join->on('products.id', '=', 'product_sort_order.product_id');
                //         $join->on('products.sub_category_id', '=', 'product_sort_order.type_id');
                //      })->orderBy('product_sort_order.sort_order', 'ASC');


            }
        ])->first();

model relationship inside category

 public function subproducts(){
  return $this->hasMany(Product::class,'sub_category_id','id');
 }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire