Eloquent Adds 'is Null' To Where Query After A Left Join

- 1 answer

Here is my eloquent query:

$table =  DB::table('invites')
       ->leftjoin('connections', 'connections.user_id', '=', 'invites.user_id')
       ->where(DB::raw('connections.firm_id = invites.firm_id'))

It should return a table of users that are connected to the firm that has invited them.

The query log shows the following query is executed:

'query' => 'select * from `invites` left join `connections` on `connections`.`user_id` = `invites`.`user_id` where connections.firm_id = invites.firm_id is null'

The is null at the end is messing everything up. I assume it's to do with the fact that the column names are the same or am I missing something with DB::raw?



where() method takes column name as first argument and if next parameters are missing, it treats it as where column is null constraint - that's why you're getting such a weird query. Use whereRaw() instead:

->whereRaw('connections.firm_id = invites.firm_id')