Ad

Query Builder Laravel If Var Null Or "" Load All Data

- 1 answer

I have a var let's say $var1

I want to run it in query builder like this

DB::table('test')->join('... `many join here` ....')->where('testA', '=', '$var1')->get();

the problem is if $var = "" or null it's not return any data

I try to follow this link @phill answer

but confused implemented in query builder laravel


@update

enter image description here

when i add $get in 3 it's not display anything

and when i check console it's error 500 (object of class can't be converted to string)

when i add $get in 2 it's can give result when $var not empty..but when $var empty it's error

where i'm missing?

Ad

Answer

You can try this way:

$var1 = "";
$q = DB::table('test')->join('... `many join here` ....');

$result = !isset($var1) ? $q : $q->where('testA', '=', $var1);
$result = $result->orderBy('column')->get();
 return $result;

You can change empty to !isset if you prefer using it.

Ad
source: stackoverflow.com
Ad