1

I have a one to many relation setup.

A Customer can have many Students, students can have a status as either Full or Left.

I want to fetch the Customer only if they have Students that are FULL.

I thought Eager Loading would do the trick but it is still returning customers if they have no Full students:

$customers = Customer::with(array('students' => function($query)
    {
      $query->where('STATUS', '=', 'FULL');
    }))->get();

Have had a hunt around on Google but couldn't find anything, not really sure how to word this question.

Thanks for your help

Pedro
  • 1,148
  • 4
  • 16
  • 35
  • does the customer also have a status field? – solidau Dec 03 '14 at 01:01
  • Nope, the customer always exists (in this case they are a Parent). They have no field to determine their status. Stuck with the table structure also so no modifications can be made to the database. – Pedro Dec 03 '14 at 01:04

1 Answers1

3

You are just selecting what relations to pull back with that query. What you want is to select the customers where a relation has some property

Try:

$customers = Customer::whereHas('students', function($q) {
    $q->where('STATUS', '=', 'FULL');
})->get();

Further reading:

http://laravel.com/docs/4.2/eloquent#querying-relations

Scopey
  • 6,269
  • 1
  • 22
  • 34