Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noamcore's avatar

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?

0 likes
0 replies

Please or to participate in this conversation.