laravel eloquent where with relationship code example
Example 1: laravel where on relation
Event::whereHas('participants', function ($query) {
$query->where('IDUser', '=', 1);
})->get();
Example 2: how to use where relationship laravel
Event::with(["owner", "participants" => function($q) use($someId){
$q->where('participants.IdUser', '=', 1);
//$q->where('some other field', $someId);
}])
Example 3: laravel how to query belongsTo relationship
$movies = Movie::whereHas('director', function($q) {
$q->where('name', 'great');
})->get();
Example 4: eloquent relationships
$roles = App\User::find(1)->roles()->orderBy('name')->get();
Example 5: query relationships laravel and select some columns
Post::query()
->with(array('user' => function($query) {
$query->select('id','username');
}))
->get();