Jay Mehta
Jay Mehta

Reputation: 11

Laravel : I am trying to pass a variable from view to controller but it returns null

This is My View code When i am executing the application it only returns me NULL value, basically my $office_category is not being passed, i need the office category to query the database

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

My Controller Code: I want the Office category thats it

Class OfficeController extends BaseController{

public function index(){

    $office_category = Input::get('office_category');
    if($office_category=='')
        $offices = Office::orderBy('office_category', 'asc')
    ->get();
    else
        $offices = Office::where('office_category','=',$office_category)
                    ->get();

    $assets = ['table','datepicker'];
    $users = User::where('client_id','=','')
        ->orderBy('name','asc')
        ->lists('name','username');

    return View::make('office.index',[
            'office_category' => $office_category,
            'offices' => $offices,
            'users' => $users,
            'assets' => $assets

            ]);
}

Where am i going wrong please help.

Upvotes: 1

Views: 430

Answers (3)

patricus
patricus

Reputation: 62228

You have a hidden field directly after your select that has the same name as the select. The value of this hidden field (empty) is what is getting sent to the server.

Delete this line:

{{ Form::hidden('office_category', $office_category) }}

Or rename this hidden field.

Upvotes: 1

Shailesh Davara
Shailesh Davara

Reputation: 81

In most of the case, above answer will solve your problem. If not, you can inspect your web request from browser and confirm value in $office_category variable.

Upvotes: 0

gmsantos
gmsantos

Reputation: 1419

By default Form::open creates a POST request and your index method on Controller are expecting a GET request.

You need to add a new route on routes.php to match this POST request.

Route::post('index', 'OfficeController@index');

Or if you don't mind, you can set index to listen any kind of request:

Route::any('index', 'OfficeController@index');

Upvotes: 0

Related Questions