Mike Barwick Mike Barwick - 2 years ago 150
PHP Question

Eloquent - where has pivot users orWhere has none

I'm trying to write a function that will get all "buckets" that are assigned to the auth'd

and/or buckets that have NO USERS assigned.

Relations and such, work as they should. Unless I'm missing something?

How can I get all buckets user is assigned too - and also include buckets where no users (including the auth user) are assigned.

  1. Buckets user is assigned to

  2. Buckets where NO users have been assigned. i.e. pivot table contains no rows for bucket, etc.

My issue very likely stems from the

$buckets = Team::currentTeam()->buckets()->with('user')->whereHas('user', function($query) {
$query->where('user_id', Auth::user()->id)
->orWhere('user_id', function() {
}, '<', 0);

Answer Source

Didn't tested this but I think this should work. You're looking for to remove that orWhere query and add orHas('user', '=', 0).

$buckets = Team::currentTeam()->buckets()->with('user')->whereHas('user', function($query) {
    $query->where('user_id', Auth::user()->id);
})->orHas('user', '=', 0)->get();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download