php - Eloquent relation pivot table with table -
i read laravel documentation couldn't understand well. have structure on database.
pricetable - contains info peiod of promotional prices , default price.
product - wich contains info products.
and pricetable_product - wich contais foreign keys of product , pricetable , respective price.
example:
pricetable | pricetable_product | product id | description | pricetable_id | product_id | price | product_id| name 1 | default | 1 | 1 | 5.00 | 1 | test 2 | promotional | 2 | 1 | 3.50 |
and @ order table can have multiples products, want know if possible relation order table, pivot table pricetable_product, because need information of table belongs price when product sold.
thanks.
first of may define relations between product , pricetable.
product model (app\product.php)
<?php namespace app; class product extends model { protected $table = 'products'; //if default primary key isn't 'id' may use $primarykey protected $primarykey = 'product_id'; public function pricetables() { return $this->belongstomany('app\pricetable'); } }
pricetable model (app\pricetable.php)
<?php namespace app; class pricetable extends model { protected $table = 'pricetable'; public function products() { return $this->belongstomany('app\product'); } }
if created relations can use:
$product = app\product::find(1); foreach ($product->pricetables $pricetable) { echo $pricetable->pivot->description; }
Comments
Post a Comment