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:
@@ -152,9 +152,9 @@ class Category extends Model implements HasMedia, Sortable
|
||||
/**
|
||||
* Category Products
|
||||
*/
|
||||
public function products(): MorphToMany
|
||||
public function products(): BelongsToMany
|
||||
{
|
||||
return $this->morphToMany(Product::class, 'productable', 'product_has_relations');
|
||||
return $this->belongsToMany(Product::class, 'category_product');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Models\Ecommerce\Product\Collection;
|
||||
use App\Models\Ecommerce\Product\Product\Product;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
use Spatie\EloquentSortable\Sortable;
|
||||
use Spatie\EloquentSortable\SortableTrait;
|
||||
@@ -109,9 +110,9 @@ class Collection extends Model implements HasMedia, Sortable
|
||||
/**
|
||||
* Products
|
||||
*/
|
||||
public function products(): MorphToMany
|
||||
public function products(): BelongsToMany
|
||||
{
|
||||
return $this->morphToMany(Product::class, 'productable', 'product_has_relations');
|
||||
return $this->belongsToMany(Product::class, 'collection_product');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -38,9 +38,9 @@ trait ProductRelationships
|
||||
/**
|
||||
* Related Channels
|
||||
*/
|
||||
public function channels(): MorphToMany
|
||||
public function channels(): BelongsToMany
|
||||
{
|
||||
return $this->morphedByMany(Channel::class, 'productable', 'product_has_relations');
|
||||
return $this->belongsToMany(Channel::class, 'channel_product');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,25 +56,25 @@ trait ProductRelationships
|
||||
/**
|
||||
* Related products (similar)
|
||||
*/
|
||||
public function relatedProducts(): MorphToMany
|
||||
public function relatedProducts(): BelongsToMany
|
||||
{
|
||||
return $this->morphedByMany(Product::class, 'productable', 'product_has_relations');
|
||||
return $this->belongsToMany(Product::class, 'product_related', 'product_id', 'related_product_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Related categories
|
||||
*/
|
||||
public function categories(): MorphToMany
|
||||
public function categories(): BelongsToMany
|
||||
{
|
||||
return $this->morphedByMany(Category::class, 'productable', 'product_has_relations');
|
||||
return $this->belongsToMany(Category::class, 'category_product');
|
||||
}
|
||||
|
||||
/**
|
||||
* Related Collections
|
||||
*/
|
||||
public function collections(): MorphToMany
|
||||
public function collections(): BelongsToMany
|
||||
{
|
||||
return $this->morphedByMany(Collection::class, 'productable', 'product_has_relations');
|
||||
return $this->belongsToMany(Collection::class, 'collection_product');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user