Lukas
Lukas

Reputation: 613

Laravel query build based on relation

I have such query:

Tournament::has('participations', '<', '2')->get();

How can I replace constant number '2' on tournament's column named slots ?? I would like retrieve only these tournaments which have least participants than slots in tournament.

Upvotes: 1

Views: 63

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 153070

Let's start by using the column name instead of "2". Like you would do in "normal" SQL

Tournament::has('participations', '<', 'slots')->get();

Now, you don't even have to try that because it won't work. But why's that? Because Laravel treats slots like a string and escapes it so SQL does as well.

What you need to do, is use DB::raw(). raw makes sure Laravel changes nothing and just injects it into the SQL query

Tournament::has('participations', '<', DB::raw('slots'))->get();

Update

After some trying out I found this: (Its not very pretty but the only way I got it working)

$subquery = function($q) use ($uid){
    $q->where('player_id', $uid);
}

Tournament::whereHas('participations', $subquery)
    ->whereHas('participations', $subquery, '<', DB::raw('slots'))
    ->get();

The first whereAs checks for count(*) > 0, the second count(*) < slots and the subquery filters by player id.

Upvotes: 3

Related Questions