Peter
Peter

Reputation: 2702

select2.js and select with content of two columns visible - laravel

I have a form and a select field in it. I also use select2.js to style the field

The select is populated by this collection

$dis = array(null => 'Please select district name...') + District::orderBy('id','asc')->lists('name_region', 'id')->all();

The code in my view is

{!! Form::select('electoraldistrict_id', $dis, Input::old('electoraldistrict_id')) !!}

Problem: Now in the select field I only see the name of the district.

How I can make the form to display BOTH id and name - so that in the field I would see sht like this:

1 - first district
2 - second district

instead of

first district
second district

Upvotes: 0

Views: 598

Answers (1)

Mike Stivala
Mike Stivala

Reputation: 111

You'll need to modify the database results to do that:

$modifiedDistricts = array_map(function($district) {
    return [
        'id' => $district['id'],
        'name_region' => "{$district['id']} - {$district['name_region']}",
    ];
}, $dis);

Upvotes: 1

Related Questions