@php // dd($prod->cross_selling_products); $crossIds = []; foreach ($prod->cross_selling_products as $crProd) { $crossIds[] = $crProd->cross_selling_product_id; } $countCsProds = 0; $term = str_slug($prod->name, ' '); // check if the product's childcategory is in `cs_category_relations` table if (!empty($prod->childcategory->category_relation)) { $sType = $prod->childcategory->category_relation->search_type; // if related with 'category' then show products under that category if ($prod->childcategory->category_relation->cs_category_type == 'App\Models\Category') { $countCsProds = \App\Models\Product::where('category_id', $prod->childcategory->category_relation->cs_category_id) ->when($sType == 'keyword', function ($query) use ($term) { return $query->whereRaw('MATCH (name) AGAINST (? IN BOOLEAN MODE)' , array($term)); }) ->whereNotIn('id', $crossIds)->count(); } // if related with 'subcategory' then show products under that subcategory elseif ($prod->childcategory->category_relation->cs_category_type == 'App\Models\Subcategory') { $countCsProds = \App\Models\Product::where('subcategory_id', $prod->childcategory->category_relation->cs_category_id) ->when($sType == 'keyword', function ($query) use ($term) { return $query->whereRaw('MATCH (name) AGAINST (? IN BOOLEAN MODE)' , array($term)); }) ->whereNotIn('id', $crossIds)->count(); } // if related with 'childcategory' then show products under that childcategory elseif ($prod->childcategory->category_relation->cs_category_type == 'App\Models\Childcategory') { $countCsProds = \App\Models\Product::where('childcategory_id', $prod->childcategory->category_relation->cs_category_id) ->when($sType == 'keyword', function ($query) use ($term) { return $query->whereRaw('MATCH (name) AGAINST (? IN BOOLEAN MODE)' , array($term)); }) ->whereNotIn('id', $crossIds)->count(); } } // check if the product's subcategory is in `cs_category_relations` table elseif (!empty($prod->subcategory->category_relation)) { $sType = $prod->subcategory->category_relation->search_type; // if related with 'category' then show products under that category if ($prod->subcategory->category_relation->cs_category_type == 'App\Models\Category') { $countCsProds = \App\Models\Product::where('category_id', $prod->subcategory->category_relation->cs_category_id) ->when($sType == 'keyword', function ($query) use ($term) { return $query->whereRaw('MATCH (name) AGAINST (? IN BOOLEAN MODE)' , array($term)); }) ->whereNotIn('id', $crossIds)->count(); } // if related with 'subcategory' then show products under that subcategory elseif ($prod->subcategory->category_relation->cs_category_type == 'App\Models\Subcategory') { $countCsProds = \App\Models\Product::where('subcategory_id', $prod->subcategory->category_relation->cs_category_id) ->when($sType == 'keyword', function ($query) use ($term) { return $query->whereRaw('MATCH (name) AGAINST (? IN BOOLEAN MODE)' , array($term)); }) ->whereNotIn('id', $crossIds)->count(); } // if related with 'childcategory' then show products under that childcategory elseif ($prod->subcategory->category_relation->cs_category_type == 'App\Models\Childcategory') { $countCsProds = \App\Models\Product::where('childcategory_id', $prod->subcategory->category_relation->cs_category_id) ->when($sType == 'keyword', function ($query) use ($term) { return $query->whereRaw('MATCH (name) AGAINST (? IN BOOLEAN MODE)' , array($term)); }) ->whereNotIn('id', $crossIds)->count(); } } // check if the product's category is in `cs_category_relations` table elseif (!empty($prod->category->category_relation)) { $sType = $prod->category->category_relation->search_type; // if related with 'category' then show products under that category if ($prod->category->category_relation->cs_category_type == 'App\Models\Category') { $countCsProds = \App\Models\Product::where('category_id', $prod->category->category_relation->cs_category_id) ->when($sType == 'keyword', function ($query) use ($term) { return $query->whereRaw('MATCH (name) AGAINST (? IN BOOLEAN MODE)' , array($term)); }) ->whereNotIn('id', $crossIds)->count(); } // if related with 'subcategory' then show products under that subcategory elseif ($prod->category->category_relation->cs_category_type == 'App\Models\Subcategory') { $countCsProds = \App\Models\Product::where('subcategory_id', $prod->category->category_relation->cs_category_id) ->when($sType == 'keyword', function ($query) use ($term) { return $query->whereRaw('MATCH (name) AGAINST (? IN BOOLEAN MODE)' , array($term)); }) ->whereNotIn('id', $crossIds)->count(); } // if related with 'childcategory' then show products under that childcategory elseif ($prod->category->category_relation->cs_category_type == 'App\Models\Childcategory') { $countCsProds = \App\Models\Product::where('childcategory_id', $prod->category->category_relation->cs_category_id) ->when($sType == 'keyword', function ($query) use ($term) { return $query->whereRaw('MATCH (name) AGAINST (? IN BOOLEAN MODE)' , array($term)); }) ->whereNotIn('id', $crossIds)->count(); } } @endphp