【问题标题】:Laravel Search Form NOT workingLaravel 搜索表单不起作用
【发布时间】:2018-01-01 01:52:26
【问题描述】:

我有两个搜索表单,一个是主页,另一个是其他页面。主页搜索表单搜索建筑物,而另一个搜索在该页面中找到的办公室。

主页搜索表单在这里工作是代码

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()!!}

这是办公室的另一种搜索表单,每当我点击搜索时它都无法正常工作,它会将我重定向回主页

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()!!}

路线

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

【问题讨论】:

  • 请显示相关路线。
  • @AlexeyMezenin 那里
  • 我已经回答了原始问题和 cmets 中的其他几个问题,所以请接受我的回答。

标签: php laravel


【解决方案1】:

您对两种搜索表单使用相同的方法。要解决这个问题,您应该创建一条新路线:

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

其中index 是您之前显示的OfficeController 中的方法。

然后使用building.blade.php中的路由:

{!! Form::open(['method'=> 'GET','url'=>'/searchoffice', 'role'=>'$searchoffice']) !!}

【讨论】:

  • @TeamPlarJarj 这意味着您已经修复了表单,但代码中还有另一个错误。您忘记将; 添加到$data['building'] = $offices。此外,您应该使用真实的视图名称而不是 view('$offices'
  • 现在它说 $offices not found,你的真实视图名称是什么意思? @AlexeyMezenin
  • @TeamPlarJarj 是的,您应该使用要渲染的视图的真实名称。喜欢view('offices', ....resources/views/offices.blade.php
  • 我的 OfficeController.php 出了什么问题,它说 View [$offices] not found.
  • @TeamPlarJarj 我在之前的评论中告诉过你这有什么问题。
猜你喜欢
  • 1970-01-01
  • 2016-03-02
  • 1970-01-01
  • 1970-01-01
  • 2014-09-02
  • 2021-10-23
  • 2017-02-09
  • 2014-10-01
  • 1970-01-01
相关资源
最近更新 更多