Level 74
That looks pretty good I think, at least from the info you told us.
Hi, Can anyone suggest if the table and relationship structure in my database is correct in the following situation: I am implementing a website with multiple stores, which basically have the same product in stock. Since the items in different stores are the same, but the price and quantity in stock in the stores are different, I did the following:
To get the price and quantity for a particular product from a particular store, I did the following:
Shop::with(['products' => function (Builder $query) use ($product_id) {
$query->where('id', $product_id);
}])->findOrFail($shop_id);
class Product extends Model
{
protected $fillable = [
'name',
'description',
];
}
class Shop extends Model
{
protected $fillable = [
'user_id',
'name',
];
public function products()
{
return $this->belongsToMany(Product::class, 'shop_stock')->withPivot('price', 'balance');
}
}
Please or to participate in this conversation.