33

I am trying to grab all records where Player's relationship called stats() has a column value of something. I would usually do ::where('column_name' 'column_value') for the players table, but how can I get ::where the relationship table's column equals to something?

Player::where('column_name', 'column_value')->get();

But I want to check a column in the relationships table?

public function roleplay()
{
    return $this->hasOne('App\Database\Frontend\User\Roleplay', 'user_id', 'id');
}
VoiD HD
  • 699
  • 2
  • 8
  • 18
  • Check this section in Laravel's documentation: https://laravel.com/docs/5.4/eloquent-relationships#querying-relationship-existence – Eduardo Pacios Mar 29 '17 at 23:31

2 Answers2

91

This will filter Player based on a related table

Player::whereHas('roleplay', function($q){
   $q->where('column_name', 'value');
})->get();
usrNotFound
  • 2,680
  • 3
  • 25
  • 41
oseintow
  • 7,221
  • 3
  • 26
  • 31
20

Laravel 8^ : doc Relationship Existence Queries

Product::with('categoreys')->whereRelation('categoreys', 'status', '0')
                                            ->get();
ali hassan
  • 321
  • 2
  • 5