Refactor product relations to use new category_product and channel_product tables
- Updated queries and relationships in various controllers and models to replace `product_has_relations` with `category_product` and `channel_product`. - Adjusted methods in `ProductFilterer`, `FilterParamsController`, and `FilterController` to reflect the new database structure. - Modified seeder to insert data into the new `channel_product` table. - Updated tests to ensure compatibility with the new product relation structure.
This commit is contained in:
@@ -38,10 +38,9 @@ class FilterParamsController extends Controller
|
||||
$category_id = (int) $request->category_id;
|
||||
|
||||
$brand_ids = DB::table('products')
|
||||
->join('product_has_relations', function ($join) use ($category_id) {
|
||||
$join->on('products.id', '=', 'product_has_relations.product_id')
|
||||
->where('product_has_relations.productable_id', '=', $category_id)
|
||||
->where('product_has_relations.productable_type', '=', 'category');
|
||||
->join('category_product', function ($join) use ($category_id) {
|
||||
$join->on('products.id', '=', 'category_product.product_id')
|
||||
->where('category_product.category_id', '=', $category_id);
|
||||
})
|
||||
->select(['id', 'brand_id'])
|
||||
->pluck('brand_id');
|
||||
|
||||
@@ -97,9 +97,8 @@ class FilterController extends Controller
|
||||
->distinct('products.id')
|
||||
->pluck('products.id');
|
||||
|
||||
return Category::where('is_visible', true)->ordered()->join('product_has_relations', 'categories.id', '=', 'product_has_relations.productable_id')
|
||||
->where('product_has_relations.productable_type', '=', 'category')
|
||||
->whereIntegerInRaw('product_has_relations.product_id', $products)
|
||||
return Category::where('is_visible', true)->ordered()->join('category_product', 'categories.id', '=', 'category_product.category_id')
|
||||
->whereIntegerInRaw('category_product.product_id', $products)
|
||||
->get(['id', 'parent_id', 'name'])
|
||||
->unique('categories.id');
|
||||
}
|
||||
|
||||
@@ -87,9 +87,8 @@ class CategoriesFilter
|
||||
->distinct('products.id')
|
||||
->pluck('products.id');
|
||||
|
||||
return $this->queryBuilder->join('product_has_relations', 'categories.id', '=', 'product_has_relations.productable_id')
|
||||
->where('product_has_relations.productable_type', '=', 'category')
|
||||
->whereIntegerInRaw('product_has_relations.product_id', $products)
|
||||
return $this->queryBuilder->join('category_product', 'categories.id', '=', 'category_product.category_id')
|
||||
->whereIntegerInRaw('category_product.product_id', $products)
|
||||
->get($this->columns)
|
||||
->unique('categories.id');
|
||||
}
|
||||
|
||||
@@ -16,18 +16,17 @@ class ProductRelatedController extends Controller
|
||||
*/
|
||||
public function index(Product $product): JsonResponse
|
||||
{
|
||||
$products = DB::table('product_has_relations')
|
||||
->select('product_has_relations.product_id')
|
||||
->whereIn('product_has_relations.productable_id', (function ($query) use ($product) {
|
||||
$query->from('product_has_relations')
|
||||
->select('productable_id')
|
||||
->distinct('productable_id')
|
||||
->where('productable_type', '=', 'category')
|
||||
$products = DB::table('category_product')
|
||||
->select('category_product.product_id')
|
||||
->whereIn('category_product.category_id', (function ($query) use ($product) {
|
||||
$query->from('category_product')
|
||||
->select('category_id')
|
||||
->distinct('category_id')
|
||||
->where('product_id', '=', $product->id);
|
||||
}))
|
||||
->limit(12)
|
||||
->orderByRaw('RANDOM()')
|
||||
->pluck('product_has_relations.product_id')
|
||||
->pluck('category_product.product_id')
|
||||
->unique();
|
||||
|
||||
return response()->rest(
|
||||
|
||||
Reference in New Issue
Block a user