Support the ongoing development of Laravel.io →
Database Eloquent

Hi all,

Due to the lack of "WHERE IN" for joins, I'm trying to add a bracketed 'OR WHERE' query, but I can't find the right way.

I need to do the following: Join Table T2 to Table T1, where ( T2.x = T1.y) and (T2.z in (1,2,3,4)). The best I got so far is

Join T2 on T2.x = T1.y and T2.z = 1 or T2.z = 2 or T2.z = 3 ..... but this is not a good solution as I need it bracketed, like: Join T2 on T2.x = T1.y and (T2.z = 1 or T2.z = 2 or T2.z = 3)

Also, something interesting, is that if I use:

$join->on("T2.x", "=", T1.y") ->where('T2.z', "IN", DB::raw("('1', '2')")); })

I get a MySQL error (SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;). However, If I copy/paste the query it provides as faulty to MySQL client - it works!! :-(

Appreciate your help, I'm stuck with this for a day now :-(

Last updated 2 years ago.
0

try to replace

DB::raw("('1', '2')")

with

DB::raw("(1, 2)")
Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ikanc ikanc Joined 25 May 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.