How to set default value in laravel collective form

Viewed 3884

i'm fetching data from database and want to make dropdown list with one value selected by default

i tried this Laravel-5 how to populate select box from database with id value and name value

but nothing happen

my view file:

<div class="row">
    <!-- Country ID Field -->
    <div class="form-group col-sm-6">
        {!! Form::label('country_id', 'Country ID:') !!}
        {!! Form::select('country_id',$countries, isset($user) ? $user->country_id : 'Nepal', ['class' => 'form-control']) !!}
    </div>

i'm new to laravel-collective..please help :)

2 Answers

The Laravel Collective code is really helpful... but it is also buggy in some odd ways.

There is an automatic binding that you can take advantage of by using null in the Collective select() constructor:

<div class="row">
    <div class="form-group col-sm-6">
        {!! Form::label('country_id', 'Country ID:') !!}
        {!! Form::select('country_id',$countries, null, ['class' => 'form-control']) !!}
</div>

This is usually really good if you use form-model binding on the forms. However, there may be cases when it doesn't pick up the user model. If so, you were correct with your original code. BUT, for some reason, Collective occasionally reads the isset section better when the isset block is within parenthesis:

<div class="row">
    <div class="form-group col-sm-6">
        {!! Form::label('country_id', 'Country ID:') !!}
        {!! Form::select('country_id',$countries, (isset($user) ? $user->country_id : 'Nepal'), ['class' => 'form-control']) !!}
</div>

Try either of these - hopefully one will help you.

The other potential item to check is to make sure your $countries are set and contain some ids, INCLUDING the id for the $user->country_id. If the user's country is not in the $countries list, it will not work even if the user's id is set.

This is bit broader than the original question about <select> inputs, but note Laravel Collective also has form model binding using the model() method, so if you were using multiple fields from your 'country' model, to use this example, you don't need to specify $countries as an argument for every input, just once at the beginning.

Instead of calling Form::open, you do:

{!! Form::model($countries, [
    'method' => 'POST',
    'url' => ['/admin/country'],
]) !!}

This is the cleanest implementation I've found, because can also use it for 'create' in a "CRUD" context; i.e. where you want to pass some default values to an otherwise empty form.

You create a new instance of your model (e.g. $country = new Country()) and set the relevant values, without calling ->save(), then use compact() to pass it to your view and call Form::model, as above, within that view.

This allows you to use the same view for creating and editing, without having to write conditionals to handle any defaults (and avoids the risk of accidentally adding a default value to an existing record if a user wipes a field).

Related