I want to replace the text match with the keyword in the input search box by the $keyword. I don't know why it doesn't work. Please see the code and help me.
I am using PHP 7.X and Laravel 5.7.
$items = DB::table('element_designs')
->join('elements', function($join){
$join->on('element_designs.id', '=', 'elements.element_design_id')
->where('elements.deleted_at', null);
})
->leftJoin('locations', function($join){
$join->on('elements.location_id', '=', 'locations.id')
->where('locations.disable_flg', false);
})
->leftJoin('areas', function($join){
$join->on('elements.area_id', '=', 'areas.id')
->where('areas.disable_flg', false);
})
->where('element_designs.disable_flg', false)
->where(function($query) use ($keyword){
$query->where('element_designs.element_name', 'LIKE', '%'.$keyword.'%')
->orWhere('locations.location_name', 'LIKE', '%'.$keyword.'%')
->orWhere('areas.area_name', 'LIKE', '%'.$keyword.'%')
->orWhere('elements.updated_at', 'LIKE', '%'.$keyword.'%');
})
->select('element_designs.id as element_design_id', 'element_designs.doc_no', 'elements.id as element_id', 'element_designs.element_name', 'locations.location_name', 'areas.area_name',
'elements.updated_at')
->paginate()
->map(function ($row) use ($keyword) {
$row->element_name = preg_replace('/' . $keyword . '/', '<strong>'.$keyword.'</strong>', $row->element_name)
->location_name = preg_replace('/' . $keyword . '/', '<strong>'.$keyword.'</strong>', $row->location_name);
return $row;
});
return view('element.data', ['items' => $items]);
When I input the key word "abc" I want the result will be strong for abc text math with the keyword. Example: "Element 1 abc" => "Element 1 abc", "Location 1 abc" => "Location 1 abc" So in the index.blade.php will show the result with strong for the text match with the keyword.