I have been working on a laravel advance search filter where a user can input several field in a form and on submit it generates data table for given fields. and if fields are empty then it just shows all the records from database (unsets the filter).
public function advanced_search(Request $request)
{
//user submitted data in post
$post = $request->all();
/*
filter array keys are defined here
e.g first_name, last_name
*/
$simple_filter = array(
"first_name" => "",
"last_name" => "",
"email" => "",
"company_name" => "",
);
foreach ($simple_filter as $key => $value) {
if (isset($post[$key]) && ($post[$key] != null) && !empty($post[$key])) {
$simple_filter[$key] = $post[$key];
} else {
//user didn't send this field in post data so removing this from filter list
unset($simple_filter[$key]);
}
}
$query = DB::table('contacts')->where($simple_filter)->get();
return DataTables::of($query)
->toJson();
}
Now this code is working fine. But requirements are changed. This code only return the exact details. Now I want to show record even if substring matches. (LIKE %data$). How can I modify this?