Miguel Stevens
Miguel Stevens

Reputation: 9201

Laravel where also returns empty results

I'm using Laravel 4 to get all the persons that have a score for a certain event

This is the query i'm using

$event = Person::with(array('eventscore' => function($q){
    $q->where('id', 3);
}))->get();

The problem is that it's also returning the persons that don't have a score in the eventscore table.

This is the output

enter image description here

Is there any way that i can return only the persons that have a score? Thanks!

Upvotes: 0

Views: 74

Answers (1)

user1669496
user1669496

Reputation: 33058

with() will not limit the Persons returned, it will only limit eventscores. If you want only Persons that have an event score, you use has or whereHas.

$event = Person::whereHas('eventscore', function($q) {
    $q->where('id', 3);
})->get();

Upvotes: 2

Related Questions