How to create Dynamic Dropdown List using laravel

Step 1 : create model

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Yourmodelname extends Model
{
protected $table = 'your_table_name';
}

Step 2 : Create Provider and Go to provider folder and copy past below
code

<?php
namespace App\Providers;
use App\Modelname; // write model name here 
use Illuminate\Support\ServiceProvider;
class Providername extends ServiceProvider
{
    public function boot()
    {
        view()->composer('*',function($view){
            $view->with('arrayname', Modelname::all());
        });
    }
 
}

Step 3 : Register your provide which we are created

Go to config/app.php and add below line

App\Providers\Providername::class,

Step 4 : Now you can used Array which we created in provider

Open your view and copy past this code

@foreach ($arrayname as $data)                                       
<option value="{{ $data->id }}"  >{{ $data->key_name }}</option>                                                      
 @endforeach

Related Posts

Divyesh Patel

I'm Divyesh Patel, Web & App developer. I want to make things that make a difference. With every line of code, i strive to make the web a beautiful place.

Leave a Reply