1

I have two search form one for the homepage and the other one is the other pages. The homepage search form searches the buildings while the other one searches the offices found in that page.

The homepage search form is working here is the code

BuildingController.php

  $search = \Request::get('search');
  $buildings = Building::where('name','like','%'.$search.'%')->orderBy('id', 'asc')->paginate();

  return view('buildings')->with('buildings', $buildings);

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...">
             <span class="input-group-btn">
               <button type="submit" class="btn btn-info btn-md">Search</i>
              </button>
           </span>
       </div>
 {!! Form::close()!!}

And this is the other search form for offices which is not working whenever I click search it redirects me back to homepage

OfficeController.php

$searchoffice = \Request::get('searchoffice');
    $offices = Office::where('name','like','%'.$searchoffice.'%')->get();
    $data['building'] = $offices
    return view('$offices',$data);

building.blade.php

{!! Form::open(['method'=> 'GET','url'=>'/','role'=>'$searchoffice']) !!}
    <div class="input-group col-xs-4 col-md-6" >
      <input type="text" name="searchoffice" class="form-control" placeholder="Search...">
     <span class="input-group-btn">
       <button type="submit" class="btn btn-info btn-md">Search</i>
       </button>
      </span>
        </div>
         {!! Form::close()!!}

Routes

Route::get('/home', 'HomeController@index')->name('home');

Route::get('/', 'BuildingController@index')->name('index');

Route::get('building/{id}', 'PageController@show');

Route::get('buildings/create', 'BuildingController@create')->name('createbform')

Route::post('building/create/store', 'BuildingController@store')->name('createbuilding');

Route::get('building/{id}/edit', 'BuildingController@edit');

Route::post('building/{id}/edit', 'BuildingController@update')->name('editbuilding');

Route::get('building/{id}/delete', 'BuildingController@destroy');

Route::get('office/{id}', 'OfficeController@show')->name('officeMenu');

Route::get('building/{id}/offices/create', 'OfficeController@create')->name('createofficeform');

Route::post('building/{id}/offices/create/store', 'OfficeController@store')->name('createoffice');

Route::get('building/{id}/offices/edit', 'OfficeController@edit')->name('editofficeform');

Route::post('building/{id}/offices/edit', 'OfficeController@update')->name('editoffice');

Route::get('offices/{id}/delete', 'OfficeController@destroy')->name('deleteoffice');
TeamPlar Jarj
  • 121
  • 1
  • 13

1 Answers1

0

You're using the same method for both search forms. To fix that you should create a new route:

Route::get('searchoffice', 'OfficeController@index');

Where index is the method in OfficeController you've shown before.

And then use the route in the building.blade.php:

{!! Form::open(['method'=> 'GET','url'=>'/searchoffice', 'role'=>'$searchoffice']) !!}
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • @TeamPlarJarj that means you've fixed the form, but you have another error in the code. You've forgot to add `;` to `$data['building'] = $offices`. Also, you should use a real view name instead of `view('$offices'` – Alexey Mezenin Jan 01 '18 at 02:11
  • now it says $offices not found, what you mean about real view name? @AlexeyMezenin – TeamPlar Jarj Jan 01 '18 at 02:17
  • @TeamPlarJarj yes, you should use the real name of the view that you want to render. Like `view('offices', ....` for `resources/views/offices.blade.php` – Alexey Mezenin Jan 01 '18 at 02:19
  • whats wrong with my OfficeController.php it says View [$offices] not found. – TeamPlar Jarj Jan 01 '18 at 02:23
  • @TeamPlarJarj I've told you what's wrong with that in the previous comment. – Alexey Mezenin Jan 01 '18 at 02:24
  • @TeamPlarJarj if you want to return `building` view, do this `return view('buildings', ['buildings' => $offices]);` instead of `return view('$offices',$data);` – Alexey Mezenin Jan 01 '18 at 02:30
  • Property [name] does not exist on this collection instance. (View: C:\xampp\htdocs\exam\resources\views\building.blade.php) now it says this @Alexey Mezenin – TeamPlar Jarj Jan 01 '18 at 02:37
  • @TeamPlarJarj that's another error you have in your view. You haven't shown the whole view, but it looks like you're trying to get `name` property on collection instead of an object. You might want to read [this](https://stackoverflow.com/questions/41366092/laravel-property-title-does-not-exist-on-this-collection-instance/41366122#41366122). – Alexey Mezenin Jan 01 '18 at 02:42