Eloquent - where there are reference users or where not

I am trying to write a function that will get all the "buckets" that are assigned auth'd userand / or buckets that don't have assigned users.

Relations, etc. work as they should. Am I missing something?

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

  • Buckets user assigned
  • Buckets where users are assigned NO . that is, the pivot table does not contain rows for the bucket, etc.

My problem is most likely related to the request orWhere...

$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();
+4
2

, , . orWhere orHas('user', '=', 0).

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

, , - left join.

Heads-up: , .

Team::currentTeam()
    ->buckets()
    ->leftJoin('users', 'users.bucket_id', '=', 'buckets.id')
    ->where(function($query) {
        $query->where('users.id', $user_id)
            ->orWhereNull('user.id');
    });

,

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

0

All Articles