3

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 :)

Tridev Shrestha
  • 447
  • 7
  • 21
  • Possible duplicate of [set a default value for select field in Laravel using collective form builder?](https://stackoverflow.com/questions/38811745/set-a-default-value-for-select-field-in-laravel-using-collective-form-builder) – Walter Cejas Jan 06 '19 at 05:33
  • i already did that..but didn't work!! – Tridev Shrestha Jan 06 '19 at 05:45
  • 1
    you don't have to use laravel collective you can do this `` just to get the things done – Islam ElHakmi Jan 06 '19 at 10:16
  • This should have worked based on their docs. Was just working with this package the other day. Also I don't think you need to use the `{!!` you can just use the regular `{{` – Mike Harrison Jan 06 '19 at 14:03

2 Answers2

1

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.

Watercayman
  • 7,970
  • 10
  • 31
  • 49
1

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).

William Turrell
  • 3,227
  • 7
  • 39
  • 57