0

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.

Thien Hoang
  • 143
  • 1
  • 17
  • I have another question with the eloquent above. How to Case-sensitive in WHERE LIKE. Example: With the element name 'Ab' I want to have a result when the user searches with ab, Ab or AB. The ->where('element_designs.element_name', 'LIKE', '%'.$keyword.'%') only have the result when I input the 'Ab'. What I should change in the where clause? Thank you so much. – Thien Hoang Apr 18 '19 at 04:25

2 Answers2

1

as far as i know map() create new collection , not using the current one , to use the current collection use transform() instead . check doc here


[UPDATE] here is the example you have asked for in the comment

Note: I am not sure about filter part of the code, you can try it and debug it you self

$items = ElementDesign::with(['elements:id,updated_at','elements.locations:location_name','elements.areas:area_name'])
        ->select('element_designs.id as element_design_id', 'element_designs.doc_no','element_designs.element_name')
        ->where('disable_fld',false)
        ->paginate();

$items = $items->getCollection()->transform(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;

})->filter(function($item) use ($keyword){


/*not sure about this part please try and debug, but the general idea should work*/
        $item->where('element_name', 'LIKE', '%'.$keyword.'%');
            ->where('locations.location_name', 'LIKE', '%'.$keyword.'%')
            ->where('areas.area_name', 'LIKE', '%'.$keyword.'%')
            ->where('elements.updated_at', 'LIKE', '%'.$keyword.'%');
    });

also you can check this answer it will be helpful about this topic

Mohammed Omer
  • 1,168
  • 1
  • 10
  • 17
0

Try this

$row->element_name = preg_replace('/' . $keyword . '/', '<strong>$keyword</strong>', $row->element_name)
                        ->location_name = preg_replace('/' . $keyword . '/', '<strong>$keyword</strong>', $row->location_name);

No concatenation needed here with tag and variables.

SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62