1

How can I do the equivalent in Eloquent (i.e. Model::where(...)->join(...)?:

DB::select('SELECT users.id, users.username
        FROM users, teams, teams AS teams2
        WHERE users.team_id = teams2.id
        AND teams.id = ?
        AND teams2.l BETWEEN teams.l AND teams.r', [$id])

1 Answer 1

1

Edited:

$team = Team::find($id);
$users = User::join('teams', 'teams.id', '=', 'users.team_id')
             ->whereBetween('teams.l', [$team->l, $team->r])
             ->get(['users.id', 'users.username']);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but unfortunately that doesn't work, it just gives me the members of the team with $id, and not the team with $id and all members of child teams.
I assume because all teams in 'teams.l BETWEEN teams.l AND teams.r' must have id $id.
Getting: Argument 2 passed to Illuminate\Database\Query\Builder::whereBetween() must be of the type array, object given
Updated, call toArray() after values().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.