Filtering pivot table data with Laravel models Filtering pivot table data with Laravel models laravel laravel

Filtering pivot table data with Laravel models


I'd suggest you create an extra method in your User model like this:

public function primaryRoles() {    return $this->roles()->wherePivot('primary', true);}

Then use something like:

$users = User::with('primaryRoles')->find(1);

Also, the "Eager Load Constraints" section in the documentation might be relevant.


Try the following:

$users = User::with(array('roles' => function($query){    $query->where('primary', 1); }))->find(1);