I'd like to get your thoughts on my current problem.
I got a model called Product this product belongs to a category and has some information like tags, attributes, etc.
Now other models like Invoice or Order can make use of Positions. And each position relates to a product.
The important thing: This position-bound-product is a copy of the original Product-model and all of it's relations (this is very important: If the original product changes, the products linked to a position must not change).
So:
Product: Describes product ("Standalone")Position: Describes positions attached to offers, orders or invoices. A position has a related product, that is a copy of a original product. This copy may not be shown on the index-page of products.
Now I try to figure out what's the best way to handle this.
At the moment I am doing this:
<?php
namespace App;
use App\Traits\FieldTrait;
/**
* Class Product
*
* @package App
*/
class Product extends Model
{
/**
* @param $query
*
* @return mixed
*/
public function scopeWithoutShadowCopies($query): mixed
{
return $query->where('position_id', null);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function position(): BelongsTo
{
return $this->belongsTo(Position::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function category()
{
return $this->belongsTo(Category::class);
}
}
This way I can add products that are "just" products and I can add products, that belong to a position. These products can be hidden by using Product::withoutShadowCopies()->get().
This allows me to create a Position and create a "shadow copy" of the model Product without showing those copies on my "Product list view".
What do you think? Is there a better way to get this working?