mysql - Passing two arguments to nested where results in "and `` is null" appended to query -
if ($input['search']) { $args = array($query, $input); $query->where(call_user_func_array(function($query, $input) { $query->where('tbl_products.name', 'like', '%' . $input['search'] . '%') ->orwhere('sku', 'like', '%' . $input['search'] . '%'); }, $args)); } } return $query; above section of query intend create nested clause resembles:
where m.name = 'name' , (p.name "% example %" or p.sku "% example %") i've made use of 'call_user_func_array' pass in multiple arguments closure (only way pass user input clause).
unfortunately receive query exception looks little this:
unknown column '' in 'where clause' ...
name%example% orsku%example% , `` null
and `` null has been appended end. think original clause needing 2 arguments i'm struggling around it. appreciated.
try this:
if ($input['search']) { $query->where(function ($query) use ($input) { $query->where('tbl_products.name', 'like', '%' . $input['search'] . '%') ->orwhere('sku', 'like', '%' . $input['search'] . '%'); }); } you can pass variables closure via use.
reference: anonymous functions.
Comments
Post a Comment