Aug 8, 2016
0
Level 11
Join only the first row
Hello, I was searching for this but I’m still in doubt if is possible.
I’ve got the tables users and addresses.
What I want to achieve is:
- Get all the users using de DB façade, with the first related address.
I don’t know if is even possible — without the DB::raw, or if a can rely on the Model to do this without DB::raw.
public function getQueryBuilder()
{
return DB::table('users')
->leftJoin('addresses', function ($join) {
$join->on('addresses.id', '=', DB::raw('(SELECT id FROM addresses WHERE addresses.user_id = users.id LIMIT 1)'));
});
}
So the question:
Can I make something like this without using DB::raw?
Please or to participate in this conversation.