在TemplateFile添加搜索

2019-08-15  本文已影响0人  浅醉Eason

需求分析:需要满足三种搜索情况1.按日期搜索 2.按关键字搜索 3.日期+关键字搜索

首先在前端模板写好样式内容template.blade.php:

 <form action="{{URL::to('/template')}}" method="get">
                                <div class="input-group input-group-md">
                                    <label for="start" class=" col-form-label mr-1">{{ __('From: ') }}</label>
                                    <input type="date" name="start" id="start" value="{{ request('start') }}" class="form-control mr-3">
                                    <label for="end" class=" col-form-label mr-1">{{ __('End: ') }} </label>
                                    <input type="date" name="end" id="end" value="{{ request('end') }}" class="form-control mr-3">
                                    <input type="text" class="form-control" name="keyword" value="{{ request('keyword') }}"
                                           placeholder="Search Template"> 
            <button type="submit" class="btn btn-primary btn-md">
                <span class="fa fa-search"></span>
            </button>
                                </div>
                                </form>

{{ request('xxxxx') }}是用来保存提交搜索后的输入框值的,因为是get方法所以表单就不用加@csrf

控制器中的逻辑TemplateController.php:

public function index(Request $request)
    {
        if ($request->has('start') && $request->start != ''||$request->has('end') && $request->end != ''||$request->has('keyword') && $request->keyword != '') {

            $keyword = $request->input('keyword');
            $start = $request->input('start');
            $end = $request->input('end');
            $data = [
                'templates' => $this->template->search($keyword,$start,$end),
            ];
                return view("template", $data);
        }
            $data = [
                'templates' => $this->template->paginate(),
            ];

            return view("template", $data);
    }

在接口App\Repositories\TemplateRepository.php写查询方法

public function search($keywords, $start, $end)
    {
        $search = Template::with('user');
        if ($keywords) {
            $search->where(function ($query) use ($keywords) {
                $query->where('template_name', 'like', '%' . $keywords . '%');
                $query->orWhere('template_content', 'like', '%' . $keywords . '%');
            });
        }
        if ($start) {
            $search->whereDate('created_at', '>=', $start);
        }
        if ($end) {
            $search->whereDate('created_at', '<=', $end);
        }
        return $search->paginate(config("app.max_page_size"));
    }
}

最后再修改下模板,处理查询结果分页以及查不到数据情况

查询不到数据时:

@if ($templates->isEmpty())
                            <span class="col-lg-auto">
        <strong class="d-flex justify-content-center text-danger pb-3">
            Whoops! No data match🤦...
        </strong>
    </span>@endif

修改获取分页数据

<div class="card-footer d-flex">
                                <div class="nav mr-auto">

                                </div>
                                <div class="nav ml-auto">
                                    {{$templates->appends(Request::only(['start','end','keyword']))->links()}}
                                </div>
                            </div>
上一篇下一篇

猜你喜欢

热点阅读