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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -