laravel 搜索表单

暂无标签
<form class="i-con-h-a col d-lg-block d-none" method="post" action="{{route('search/list')}}">
{{csrf_field()}}
<i class="i-con i-con-search text-fade w-16"></i>
<input type="text" class="form-control search-input" name="keyword" value="{{Request::input('keyword')}}" placeholder="Search...">
</form>

控制器

public function list(Request $request)
{
    if($request->isMethod('POST')){
        if($request->keyword) {
            $where = function ($query) use ($request) {
            $search = "%" . $request->keyword . "%";
            $query->where('title', 'like', $search);
        };
        }else{
            return view('tips/showmessage')->with(['message'=>'缺少搜索关键词','pagetitle'=>'首页','url' =>'/', 'jumpTime'=>2,'type'=>'error']);    
        }
        $search_lists = Article::where($where)->paginate(20);
        return view('search/search_list',['search_lists'=>$search_lists]);
    }else{
        return view('tips/showmessage')->with(['message'=>'搜索首页没做,不要直接访问好吧','pagetitle'=>'首页','url' =>'/', 'jumpTime'=>2,'type'=>'error']);
    }
}

结果列表

@if($search_lists->total())
@foreach($search_lists as $value)
<li><a href="{{route('article/detail',['alias'=>$value->alias])}}" target="_blank">{!! str_replace(Request::input('keyword'), '<span class="text-danger font-weight-bold">'.Request::input('keyword').'</span>', $value->title) !!}</a></li>
@endforeach
@else
没有找到关于<span class="text-danger font-weight-bold">{{Request::input('keyword')}}</span>的结果
@endif

其中,关键词替换(高亮)

{!! str_replace(Request::input('keyword'), '<span class="text-danger font-weight-bold">'.Request::input('keyword').'</span>', $value->title) !!}