php - How wherePivot() actually works internally in laravel 5? -
how wherepivot() works internally in laravel 5 ?
for example practicing watching tutorial , teacher using wherepivot() construing relationship:
public function friendsofmine(){ return $this->belongstomany('chatty\models\user','friends','user_id','friend_id'); } public function friendof(){ return $this->belongstomany('chatty\models\user','friends','friend_id','user_id'); } public function friends(){ return $this->friendsofmine()->wherepivot('accepted',true)->get()->merge($this->friendof()->wherepivot('accepted',true)->get()); }
thanks guys .. think found answer
a pivot table database table exists serve many-to-many relationship. have table “customer” , table “drinks”. if want know customer ordered drink have create pivot table customer_drinks(customer_id, drink_id).
define pivot table
class customer extends \eloquent { public function drinks() { return $this->belongstomany('drink', 'customer_drinks', 'customer_id', 'drink_id'); } } create record
$customer = customer::find($customer_id); $customer->drinks()->attach($drink_id); //this executes insert-query remove record pivot table
$customer = customer::find($customer_id); $customer->drinks()->detach($drink_id); //this executes delete-query on pivot table
Comments
Post a Comment