php - laravel 5.3 raw SQL statement in Builder -
i have applied laravel builder filter search input user in form, isn't working optimal. scenario:
in first input custom query search following builder made.
return $builder->where('city', 'like', '%' . $value . '%') ->orwhere('first_name', 'like', '%' . $value . '%') ->orwhere('middle_name', 'like', '%' . $value . '%') ->orwhere('last_name', 'like', '%' . $value . '%');
the 3 dropdown menu's filtered following code:
return $builder->where('location', $value); // <-- "selecteer locatie" return $builder->where('level', $value); // <-- "selecteer richting" return $builder->where('graduation', $value); // <-- "selecteer diplomajaar"
$value input of user.
now whenever filter 3 dropdown options, works magic. whenever try combine query of first filter, shows more results want.
so first query giving or-statement , bug. want place raw sql statement in builder can run
"where `city` %" . $value . "% or `first_name` %" . $value . " or `middle_name` %" . $value . " or `last_name` %" . $value . "
in between these symbols ( 'statement' )
is there way can run builder separately or way how can input raw sql statement in laravel 5.3 builder?
if need more information, please ask.
edit: question not clear enough! see description below.
i using builder class make sql filter in form. whenever run advanced search , dump builder, builder object following array in +where clause:
+wheres: array:7 [▼ 0 => array:5 [▼ "type" => "basic" "column" => "city" "operator" => "like" "value" => "%hof%" "boolean" => "and" ] 1 => array:5 [▼ "type" => "basic" "column" => "first_name" "operator" => "like" "value" => "%hof%" "boolean" => "or" ] 2 => array:5 [▼ "type" => "basic" "column" => "middle_name" "operator" => "like" "value" => "%hof%" "boolean" => "or" ] 3 => array:5 [▼ "type" => "basic" "column" => "last_name" "operator" => "like" "value" => "%hof%" "boolean" => "or" ] 4 => array:5 [▼ "type" => "basic" "column" => "location" "operator" => "=" "value" => "franeker" "boolean" => "and" ] 5 => array:5 [▼ "type" => "basic" "column" => "level" "operator" => "=" "value" => "mavo" "boolean" => "and" ] 6 => array:5 [▼ "type" => "basic" "column" => "graduation" "operator" => "=" "value" => "1992" "boolean" => "and" ] ]
because using or-operator comparing city
, first_name
, middle_name
or last_name
, should getting 4 clauses instead of 7. caused because builder sees or-statement called in first input and-statement.
i solved replacing this:
return $builder->where('city', 'like', '%' . $value . '%') ->orwhere('first_name', 'like', '%' . $value . '%') ->orwhere('middle_name', 'like', '%' . $value . '%') ->orwhere('last_name', 'like', '%' . $value . '%');
into this:
return $builder->where( function($query) use ($value){ $query->where('city', 'like', '%' . $value . '%') ->orwhere('first_name', 'like', '%' . $value . '%') ->orwhere('middle_name', 'like', '%' . $value . '%') ->orwhere('last_name', 'like', '%' . $value . '%'); });
edit: following +where clauses dumped when die , dumping builder class
+wheres: array:4 [▼ 0 => array:3 [▼ "type" => "nested" "query" => builder {#216 ▶} "boolean" => "and" ] 1 => array:5 [▼ "type" => "basic" "column" => "location" "operator" => "=" "value" => "franeker" "boolean" => "and" ] 2 => array:5 [▼ "type" => "basic" "column" => "level" "operator" => "=" "value" => "mavo" "boolean" => "and" ] 3 => array:5 [▼ "type" => "basic" "column" => "graduation" "operator" => "=" "value" => "1992" "boolean" => "and" ] ]
in nested builder class @ array index 0 or-statement inserted.
Comments
Post a Comment