Thursday, March 30, 2017

Laravel: WhereNull, OrWhere, is ignoring WhereRaw

In my controller I am using Laravels WhereNull, OrWhere and WhereRaw in DB Query. It is pulling in all the results but the problem is it looks like it is pulling in everything and ignoring the last Where clause. I have used this in a different method on other controllers and it works fine. Is there a specific order or something I am missing?

Doesn't Work (Ignores WhereRaw and shows all results)

$lists = DB::table('statuses')
                    ->whereNull('company_id')
                    ->orWhere('company_id', '=', Auth::user()->company_id)
                    ->whereRaw("FIND_IN_SET('Task',assigned_to)")
                    ->get();

This works in other controllers used as a different method without the whereRaw:

return Status::whereNull('company_id')->orWhere('company_id', '=', Auth::user()->company_id)
                    ->orderBy('created_at', 'asc')
                    ->get();

Sample DB enter image description here

Source link: https://laravel.com/docs/5.3/queries#where-clauses



via Derek

Advertisement