0

Im new to laravel and I want to populate my dropdown list using Form Collectives in Laravel

Fore example, Here is my array of countries

<?php 
    $countries = array("AF" => "Afghanistan",
    "AL" => "Albania",
    "DZ" => "Algeria",
    "AS" => "American Samoa",
    "AD" => "Andorra",
    "AO" => "Angola")
?>

Here is my Form-Collective "Select / Dropdown"

Using Form Collective

   {{Form::select('country',  '', null, ['class' => 'form-control', 'placeholder' => 'Select Country...'])}}

So how can i do it? Anyone who would like to help me, I appreciate it thank you!

1 Answer 1

2

The second parameter in the Form collective select function receives an array of values that you want to be displayed, so simply pass your array, and change {{ with {!! which escapes the HTML output instead of printing it out as text.

   {!! Form::select('country',  $countries, null, ['class' => 'form-control', 'placeholder' => 'Select Country...']) !!}

--- EDIT

If you don't have an admin panel from which you enter a country, then the simplest way with which I will go is store the countries in a language file. For example:

in resources/lang/en/countries.php

return [
   "AF" => "Afghanistan",
   "AL" => "Albania",
   "DZ" => "Algeria",
   "AS" => "American Samoa",
   "AD" => "Andorra",
   "AO" => "Angola"
];

then in your view:

{!! Form::select('country',  trans('countries'), null, ['class' => 'form-control', 'placeholder' => 'Select Country...']) !!}
Sign up to request clarification or add additional context in comments.

4 Comments

It does not work :( . I think there's something wrong or It needed to add something
@SummerWinter How are you passing the array, or what error do you get? Have you tried inline if it will work, such as this {!! Form::select('country', ["AF" => "Afghanistan", "AL" => "Albania"], null, ['class' => 'form-control', 'placeholder' => 'Select Country...']) !!}
Yeah it work like that .. but im just wondering if there's a short way on how to generate or store the array like the simplest and practical way
It does work !! thank you for helping beginners like me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.