我有这个href重定向回主页面。问题发生在我搜索内容中没有找到的东西时,它显示没有发现任何杂乱,它应该这样做但事情就是当我点击返回到href并且链接是正确但它没有显示主页我必须点击两次href来显示页面的内容。这就是我做的事情
这里是代码
BuildingController.php
public function index()
{
$search = \Request::get('search');
$buildings = Building::where('name','like','%'.$search.'%')->orderBy('id', 'asc')->paginate();
if(!$buildings || !$buildings->count()){
Session::flash('no-results', 'NOTHING FOUND');
}
return view('buildings')->with('buildings', $buildings);
}
主页的代码
<a class="navbar-brand" href="{{ url('/') }}">homepage</a>
buildings.blade.php
{!! Form::open(['method'=> 'GET','url'=>'/','role'=>'search']) !!}
<div class="input-group col-xs-4 col-md-6" >
<input type="text" name="search" class="form-control" placeholder="Search..." required>
<span class="input-group-btn">
<button type="submit" class="btn btn-info btn-md"><span class="glyphicon glyphicon-search"></span> Search</i>
</button>
</span>
</div>
{!! Form::close()!!}
</center>
<center>
@if(Session::has('no-results'))
<span>{{ Session::get('no-results') }}</span> </center>
@else
<div class="container">
<div class="table responsive-vertical">
<table class="table table-bordered table striped table-hover table-mclight-blue">
<thead>
<tr>
<th>Building @if(!Auth::guest())
<a href="{{route('createbform')}}" class="btn btn-link btn-sm"> <span class="glyphicon glyphicon-plus"></span> Add Building</a>
@endif
</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach($buildings as $building)
<tr>
<td data-title="Building">{{$building->name}}</td>
<td>
<a href="{{url('building', $building->id)}}" class="btn btn-primary btn-sm" title='Offices'><span class="glyphicon glyphicon-briefcase"></span> Offices</a>
@if(!Auth::guest())
<a href="{{url('building', $building->id)}}/edit" class="btn btn-success btn-sm" title='Edit'> <span class="glyphicon glyphicon-edit"></span></a>
<a href="{{url('building', $building->id)}}/delete" class="btn btn-danger btn-sm" title='Delete'> <span class="glyphicon glyphicon-trash"></span></a>
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
{{$buildings->links()}}
@endif
@endsection
答案 0 :(得分:0)
那是因为即使没有搜索结果你也会返回视图!如果没有搜索结果,请不要返回视图
public function index()
{
$search = \Request::get('search');
$buildings = Building::where('name','like','%'.$search.'%')->orderBy('id', 'asc')->paginate();
if(!$buildings || !$buildings->count()){
Session::flash('no-results', 'NOTHING FOUND');
}else{ //added this else statment
return view('buildings')->with('buildings', $buildings);
}
}