Hi,
How can I embed an If else condition inside a db query. If the test value is true then it must ignore the where clause.
DB::table(‘products’)
->join(‘brand’, ‘products.brandid’, ‘=’, ‘brand.id’)
->join(‘menu’, ‘products.menuid’, ‘=’, ‘menu.menukey’)
->select(
‘products.id AS prodid’,
‘products.menuid’,
)
->whereRaw(…// this must be ignored if test is true)
->orderBy(‘prodid’, ‘asc’)
->limit(1000)
->get();
Sorry can I rephrase, need to test for condition outside of the query using IF. If the test is true, then only include the where in the query.( I would like to include exclude where conditions in the query according to the test)
You can create another when method next to the previous one or within the callback function, or you can return based on conditions you have.
As you’re using Laravel’s query builder, I would suggest to read through their docs too, everything is explained there pretty well: https://laravel.com/docs/7.x/queries