Home > Software engineering >  Laravel Query Logic
Laravel Query Logic

Time:05-19

I have problems with Laravel query. So I have a table that have 4 properties that I need to check, and table looks like:

product_id | partner_id | employer_id | type

So partner_id is setted in Config and there can be multiple different partners, as well as products and employers, type can be white or black. Employers are array so I need to check them with whereIn or whereNotIn.

So what I need to check is, where type is white, and not current partner and not current employers to list all rows from that table.

So I did something like

ProductListing::whereNotIn('employer_id', $employers)
->where('type', 'white')
->where('partner_id', '!=', $partner_id)
->pluck('product_id')
->toArray(); 

But this query is not working as expected. I should probably check every row for partner and then list whereNotIn, but I have no idea how. Any ideas how to fix it?

CodePudding user response:

Use where with a != operator in combination with whereNull.

where('employer_id', '!=' , $employers)->orWhereNull('employer_id')

CodePudding user response:

you can check Basic Where Clauses in docs to see that inequality sign is <>

// from docs
$users = DB::table('users')
                ->where('votes', '<>', 100)
                ->get();

// your fixed query 
ProductListing::whereNotIn('employer_id', $employers)
->where('type', 'white')
->where('partner_id', '<>', $partner_id)
// as you mentioned that you want to list all suitable rows,not just product ids
->get(); 
  • Related