Q: 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 user 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.


*

*Buckets user is assigned to

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


My issue very likely stems from the orWhere query...
$buckets = Team::currentTeam()->buckets()->with('user')->whereHas('user', function($query) {
    $query->where('user_id', Auth::user()->id)
    ->orWhere('user_id', function() {
        $query->count();
    }, '<', 0);
})->get();


A: Didn't tested this but I think this should work. You're looking 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();


A: Another possible solution you might consider is using a left join.
Heads-up: this might not be accurate as I don't know your db schema.
Team::currentTeam()
    ->buckets()
    ->leftJoin('users', 'users.bucket_id', '=', 'buckets.id')
    ->where(function($query) {
        $query->where('users.id', $user_id)
            ->orWhereNull('user.id');
    });

Please also check this blog post 

AND-OR-AND + brackets with Eloquent
  https://laraveldaily.com/and-or-and-brackets-with-eloquent/

