[英]Highlight search results in laravel 5.5
I am usiong laravel 5.5, I have a blade template in which I have a search, I want when the user types a keyword so the results comes with highlighted keywords.我是 usiong laravel 5.5,我有一个刀片模板,我可以在其中进行搜索,我希望用户输入关键字时,结果会带有突出显示的关键字。 I have a code which works fine but when i try to loop it, shows me an error:我有一个运行良好的代码,但是当我尝试循环它时,显示了一个错误:
Invalid argument supplied for foreach()为 foreach() 提供的参数无效
my controller:我的控制器:
$keyword = $request->name;
$searchres = DB::table('brands')
->select('*')
->where('name', 'LIKE', "%$keyword%")->get();
$search = preg_replace("/($keyword)/i", "<b>$1</b>", $searchres);
return view('frontend.ft_list', compact('search'));
blade template:刀片模板:
@foreach($search as $result)
<div class="card" style="border-radius: 1rem;width: 100%">
<h5 class="card-header" style="text-align: center; font-weight: bold">{{$result->name}}</h5>
<div class="card-body">
<h5 class="card-title">{{$result->published}}</h5>
<p class="card-text" style="text-align: center;font-weight: bold">{{$result->user_id}}</p>
</div>
</div>
<br>
<br>
@endforeach
I would really appreciate if someone could help me with it.如果有人可以帮助我,我将不胜感激。 Thanks in advance!!提前致谢!!
Ok i have an idea and i hope that it going to help you好的,我有一个想法,希望对你有帮助
$keyword = $request->name;
$searchres = DB::table('brands')
->select('*')
->where('name', 'LIKE', "%$keyword%")
->get()
->map(function ($row) use ($keyword) {
$row->name = preg_replace('/(' . $keyword . ')/i', "<b>$1</b>", $row->name);
return $row;
});
return view('frontend.ft_list', compact('searchres'));
and in the blade use并在刀片使用中
{!! $yourvariable !!}
and don't use并且不要使用
{{ $yourvariable }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.