php - Laravel selecting data with multiple where clauses and returning any pivot data -
i have relationship in app organisation can have many users , many users can have many organisations, in model looks this,
organisation.php
public function users() { homecoming $this->belongstomany('user')->withpivot('is_admin'); }
user.php
public function organisations() { $this->belongstomany('organisation'); }
what wanting do, run query has multiple clauses , homecoming row info , pivot data.
for 1 clause following,
$org = organisation::find(1);
$orgusers = $org->users()
;
what cannot figure out how user multiple wheres if needed too?
assuming want add together wheree()
's users, might (eager loading users)
$constraints = ['fname' => 'john', 'lanme' => 'doe']; $org = organisation::with(['users' => function($query) utilize ($constraints){ foreach($constraints $field => $val) $query->where($field, $val); // or if want admin users using pivot table field $query->pivot->where('is_admin', true); }])->find($id);
or, them all, , filter them if need collection of users, , collection of admins
$org = organisation::with('users')->find($id); $admins = $org->users->filter(function($user){ homecoming $user->pivot->is_admin });
php mysql database laravel eloquent
No comments:
Post a Comment