I want to make a relation between my discounts where product_id is same as with my product_id in the pivot of the multiprice and avoid that discount attends to the all multiprice in other products
my discount model:
class Discount extends Model
{
public function product()
{
return $this->belongsTo('App\Product','product_id');
}
public function material()
{
return $this->belongsTo('App\Material','material_id');
}
}
multiprice model:
class Multiprice extends Model
{
public function products()
{
return $this->belongsToMany('App\Product');
}
public function material()
{
return $this->belongsTo('App\Material','material_id')->select('id','title');
}
public function discount()
{
return $this->hasOne('App\Discount','multiprice_id');
}
}
here is my function for show:
$product = Product::where('barcode', $barcode)->with('collection.products','inventory','category','catalog','brand','material','size','weight','multiprices.discount','discount','images')->first();
return $product;
and here is a sample JSON that I return but you can see that the product_id of pivot in multiprice is not matched to product_id if discount and its effect in every product multiprice, not the only one product with same id.
multiprices: [
{
id: 11,
name: "مخمل لطیف",
price: "1600000",
sort_order: 17,
desc: "رومیزی سایز 100 در 100",
slug: null,
size_id: 6,
cat_id: 7,
material_id: 6,
created_at: "2019-12-21T08:37:27.000000Z",
updated_at: "2020-06-27T14:54:40.000000Z",
pivot: {
product_id: 2280,
multiprice_id: 11
},
discount: {
id: 645,
name: "",
desc: null,
price: "320000",
uses: 0,
max_uses: 1,
is_fixed: 0,
active: 1,
product_id: 2250,
material_id: 6,
multiprice_id: 11,
created_at: "یکشنبه 02 شهریور 1399 ساعت 19:04",
updated_at: "2020-08-23T12:38:33.000000Z",
delete: 0
}
},