Fly_Moe
Fly_Moe

Reputation: 307

Laravel - ORM where clause for eager loading

Here my ORM that I'm running:

$developers = Developer::where('name', 'LIKE', '%'.$query.'%')->with('programs')->get();

The model for Developer is this:

public function programs()
{
   return $this->hasMany('App\Program');
}

This is bringing back all the names from the Developer table not from the programs tables. How would I go about trying to get all the the program names instead of the developer names?

Upvotes: 0

Views: 47

Answers (1)

void
void

Reputation: 913

Try Like this:

$query = "hello";
$developers = Developer::whereHas('programs', function($q) use($query){
    $q->where('name', 'LIKE', '%'.$query.'%');
})->get();

It will help you!

Upvotes: 1

Related Questions