How to set default value in laravel collective form

Tridev Shrestha

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

Watercayman

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.

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

How to set default value to None for Django choices form field

Remove tiresome <optgroup> in form builder in laravel collective

How to set default value if joining table data not exist in laravel?

How to set default value in html for <select> element using Laravel

Resetting form and set default value Angular 2

How to set default value in function

How to set default value for variable?

How to set form2 as default?

Confirme a exclusão usando sweetalert no Laravel Form Collective

Adicionando classe condicionalmente dentro de Form :: text () no Laravel Collective

How to override a form default value with parsed typoscript

Angular 7: How to set default value on Reactive Form mat-select when value is an object with several IDs rather than a single ID?

Django Admin Form: Set the default value of a readonly field

Add default value to select list in Laravel form::select

How to set a default value in a dropdown javascript

How to set default value in materialize autocomplete input?

how to set initial(default) value in dropdownButton?

How to set default value of <p:selectOneMenu

How to set default value for radio button in react?

how to set column in GridView to default value?

Laravel form inputs - how to set maxlength

Laravel 5 set default value to Select2

How to set MenuSelect value to a boolean in a Formik form?

How to set my first value (default value) in spinner as empty

How to add custom field in default registration form of Laravel 5.6?

Set default value of validation

Laravel Populate lista suspensa de países usando Form Collective using array

How to set default locale without prefix in Laravel 8

How to set default uuid on save model Laravel 5.3+?

TOP lista

  1. 1

    R Shiny: use HTML em funções (como textInput, checkboxGroupInput)

  2. 2

    O Chromium e o Firefox exibem as cores de maneira diferente e não sei qual deles está fazendo certo

  3. 3

    Como assinar digitalmente um documento PDF com assinatura e texto visíveis usando Java

  4. 4

    R Folheto. Dados de pontos de grupo em células para resumir muitos pontos de dados

  5. 5

    Gerenciar recurso shake de Windows Aero com barra de título personalizado

  6. 6

    Como obter dados API adequados para o aplicativo angular?

  7. 7

    UITextView não está exibindo texto longo

  8. 8

    Por que meus intervalos de confiança de 95% da minha regressão multivariada estão sendo plotados como uma linha de loess?

  9. 9

    Acessando relatório de campanhas na AdMob usando a API do Adsense

  10. 10

    Usando o plug-in Platform.js do Google

  11. 11

    Como posso modificar esse algoritmo de linha de visada para aceitar raios que passam pelos cantos?

  12. 12

    Dependência circular de diálogo personalizado

  13. 13

    Coloque uma caixa de texto HTML em uma imagem em uma posição fixa para site para desktop e celular

  14. 14

    iOS: como adicionar sombra projetada e sombra de traço no UIView?

  15. 15

    Como usar a caixa de diálogo de seleção de nomes com VBA para enviar e-mail para mais de um destinatário?

  16. 16

    Tabela CSS: barra de rolagem para a primeira coluna e largura automática para a coluna restante

  17. 17

    How to create dynamic navigation menu select from database using Codeigniter?

  18. 18

    Converter valores de linha SQL em colunas

  19. 19

    ChartJS, várias linhas no rótulo do gráfico de barras

  20. 20

    用@StyleableRes注释的getStyledAttributes。禁止警告

  21. 21

    não é possível adicionar dependência para com.google.android.gms.tasks.OnSuccessListener

quentelabel

Arquivo