angelina98's avatar

Dependent dropdown from same table

Please help me to make Dependent dropdown from same table with Laravel 5

0 likes
3 replies
Tray2's avatar

Not sure what you are after here but to make a dropdown populated by the database I do something like this.

//Controller

public function index()
{
    $models = Model::All();
    return view('model.index', compact('models');
}

//View

<select>
    @foreach($models as $model)
        <option value="{{ $model->id }}">{{ $model->field }}</option>
    @endforeach
</select>
angelina98's avatar

i want to make 2 dropdown (dropdown keyword and subkeyword) from same table and dependent each other like dropdown country and city ... Please Help me :')

Tray2's avatar

If it's the same table you need some kind of hierachy like.

id item         parent_id
 1  Country     null
 2 City              1

Then you can get all the records where the parent_id is null for one dropdown. Then you can use ajax to load the data into the second dropdown based on the selected id = parent_id

Please or to participate in this conversation.