Laravel:我试图将一个变量从视图传递到控制器,但它返回null
这是我执行应用程序时的视图代码,它只返回空值,基本上没有传递$office\u类别,我需要office类别来查询数据库Laravel:我试图将一个变量从视图传递到控制器,但它返回null,laravel,view,laravel-4,controller,Laravel,View,Laravel 4,Controller,这是我执行应用程序时的视图代码,它只返回空值,基本上没有传递$office\u类别,我需要office类别来查询数据库 <div class="box-body"> {{ Form::open(['route' => 'office.index','class' => 'form-horizontal office-form']) }} <div class="form-body">
<div class="box-body">
{{ Form::open(['route' => 'office.index','class' => 'form-horizontal office-form']) }}
<div class="form-body">
<div class="form-group">
<div class="col-md-3">
{{ Form::select('office_category', [
null=>'Please Select',
'Software' => 'Software',
'Computer Hardware' => 'Computer Hardware',
'Survey Instruments' => 'Survey Instruments',
'Office Equipments' => 'Office Equipments'
], isset($office_category) ? $office_category : '', ['class' => 'form-control input-xlarge select2me', 'placeholder' => 'Project Type', 'id' => 'office_category'] ) }}
</div>
{{ Form::hidden('office_category', $office_category) }}
{{ Form::submit('Search Equipment',['class' => 'btn green']) }}
</div>
</div>
{{ Form::close() }}
我哪里出错了请帮助。默认情况下
Form::open
和控制器上的索引方法需要GET
请求
Route::post('index', 'OfficeController@index');
您需要在routes.php
上添加新路由,以匹配此POST
请求
Route::post('index', 'OfficeController@index');
或者,如果您不介意,您可以设置index
来侦听任何类型的请求:
Route::any('index', 'OfficeController@index');
在大多数情况下,上述答案将解决您的问题。如果没有,您可以从浏览器检查您的web请求,并确认$office_类别变量中的值。您的select后面有一个隐藏字段,该字段与select具有相同的名称。此隐藏字段的值(空)是发送到服务器的内容 删除此行:
{{ Form::hidden('office_category', $office_category) }}
或者重命名此隐藏字段。OP没有提到404 for MethodNotAllowedHttpException。我不认为路线是问题所在。