Laravel如何设置输入字段为空搜索结果不显示,默认不显示搜索结果。
1. 问题描述
我正在使用 Laravel 进行搜索功能,但是Laravel 页面没有任何搜索输入,所有结果都会显示出来。
怎么设置让输入空白时不显示搜索结果
现在默认打开搜索前端网页就会直接显示所有搜索结果。
没有输入任何条件,没有提交。
2. 运行环境
debian9
1). 当前使用的 Laravel 版本?
laravel7
2). 当前使用的 php/php-fpm 版本?
php7
3). 当前系统
debian9
3). 您期望得到的结果?
我希望默认打开页面默认不显示 搜索结果,输入条件,提交后台,前端显示搜索结果
<div style="text-align:center;vertical-align:middel;">
<form action="" method="GET" >
<input type="text" required="required" autofocus="" name="q" style="width:400px; height:26px;"placeholder="{{$q}}" >
<input type="submit" value="Search" style="width:100px; height:26px;">
</form>
</div>
</div>
<hr>
<ul id="content-list">
@foreach($sites as $v)
@php
$data = 1;
@endphp
<li>
<article class="content-item" @if($v->img)style="background-image:url('{{url('upload/'.$v->img)}}');"@endif >
<header>
<h1><a href="{{route('vote')}}?id={{$v->id}}">{{$v->name}}</a></h1>
</header>
</article>
</li>
@endforeach
@if(!isset($data))
<div class="centerme"><h1>No pages found<h1></div>
@endif
</ul>
{{ $sites->links() }}
</div>
public function search(Request $request)
{
$data = $request->all();
if(isset($data['p']))
$request->request->add(['page'=>$data['p']]);
$q = (isset($data['q']))? $data['q']:'';
$sites = Website::with('comments')
->withCount([
'comments as up' => function($query){
$query->where('attitude',1);
},
'comments as up7' => function($query){
$date=date_create();
date_modify($date,"-7 days");
$query->where('created_at','>',date_format($date,"Y-m-d H:i:s"))
->where('attitude',1);
},
'comments as down' => function($query){
$query->where('attitude',2);
},
'comments as down7' => function($query){
$date=date_create();
date_modify($date,"-7 days");
$query->where('created_at','>',date_format($date,"Y-m-d H:i:s"))
->where('attitude',2);
},
])
->where([['state','>',0],['name','like',"%$q%"]])
->orWhere([['state','>',0],['url','like',"%$q%"]])
->orWhere([['state','>',0],['keywords','like',"%$q%"]])
->orWhere([['state','>',0],['description','like',"%$q%"]])
->orderBy('sort','desc')
->orderBy('state','desc')
->orderBy('up7','desc')
->orderBy('down7','asc')
->orderBy('up','desc')
->orderBy('created_at','desc')
->orderBy('down','asc')
->paginate(15);
foreach($sites as $v){
if(!$v->up7 && !$v->down7)
$v->up7s = 0;
elseif(!$v->down7)
$v->up7s = 100;
else
$v->up7s = min(floor(($v->up7/($v->down7+$v->up7))*100), 100);
}
$arr = [
'sites' => $sites,
'q' => $q,
];
return view('search', $arr);
推荐文章: