Back button/homepage href not working need to click twice to show content, Laravel

223 views Asked by At

I have this href that redirects back to main page. The problem occurs when I search something which is not found in the content it shows the nothing found messsage,its supposed to do that but the thing is when I click back to the href and the link is right but it doesn't show the homepage I have to click twice the href to show the contents of the page. here's what I did

here's the code

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);
    }

the code for the homepage

<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
1

There are 1 answers

0
Mahdi Younesi On

That's because you are returning view even when there is not search result ! When there is no search result don't return the view

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);

  }
}