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

mohammadbasir's avatar

MySql query to Laravel Eloquent conversion.

Helo

I want to convert this query to laravel eloquent. Any suggestion how to do that?

SELECT * 
FROM chat 
WHERE (id_participant1 = 1 OR id_participant2 = 1)
  AND (id_participant1 = 2 OR id_participant2 = 2)

0 likes
2 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@mohammadbasir If you want to convert your raw query to eloquent, you need to use closures for that.

I can think of this one as of now. I guess it will be working. Try.

DB::table('chat')
   ->where(function($query) {
        $query->where('id_participant1', 1)
            ->orWhere('id_participant2', 1);
    })
   ->where(function($query) {
        $query->where('id_participant1', 2)
            ->orWhere('id_participant2', 2);
    })
   ->get();

Plz take a note that, the code hasn't tested. You might need to adjust.

3 likes

Please or to participate in this conversation.