I guess you can use the WHERE NOT IN
query and pass in an array of ids of all related models.
Is there are way to generate such an array via eloquent? something like:
$id_array = Game::get()->id->toarray();
This should give you all the entries from game table, that are not equal to the specific user_id in the game table.
User::find($id)->games()->where('user_id', '!=', $id)->get();
DavidDomain said:
This should give you all the entries from game table, that are not equal to the specific user_id in the game table.
User::find($id)->games()->where('user_id', '!=', $id)->get();
This is actually not working as well. Since you select all games which for sure have user_id == $id, the result should allways be NULL.
There is no function from laravel to run this out of the box.
You have to join this manually:
$games = Game::leftJoin('game_user', function($join) use ($userId){
$join->on('games.id', '=', 'game_users.game_id');
$join->on('game_users.user_id', '=', \DB::raw("'".$userId."'"));
})->whereNull('game_users.game_id')->get();
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community