0

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?

Community
  • 1
  • 1
Surya Matadewa
  • 1,017
  • 5
  • 19
  • 38

1 Answers1

1

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.

ssuhat
  • 7,387
  • 18
  • 61
  • 116