How to create dropdown in laravel?

by dalton_moen , in category: PHP Frameworks , 9 months ago

How to create dropdown in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by filiberto , 9 months ago

@dalton_moen 

To create a dropdown in Laravel, you can use the Form facade provided by Laravel Collective. Here's an example of how you can create a dropdown in a Laravel blade template:

  1. Install Laravel Collective HTML package by running the following command:
1
composer require "laravelcollective/html":"^6.0"


  1. Add the HTML and FORM facades to the config/app.php file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
'providers' => [
    // Other service providers...
    CollectiveHtmlHtmlServiceProvider::class,
  ],

'aliases' => [
    // Other aliases...
    'Form' => CollectiveHtmlFormFacade::class,
    'Html' => CollectiveHtmlHtmlFacade::class,
  ],


  1. Create a form in your blade template:
1
{!! Form::select('dropdown_name', $options, null, ['class' => 'form-control']) !!}


In this example:

  • dropdown_name is the name attribute of the dropdown
  • $options is an array of key-value pairs where the key is the option value and the value is the option label
  • null is the selected option (you can pass a default value here)
  • ['class' => 'form-control'] is an array of HTML attributes for the dropdown
  1. Don't forget to submit the form to store the selected value in your database.


That's it! You have successfully created a dropdown in Laravel using Laravel Collective.