maunoxyd
maunoxyd

Reputation: 286

Multiple select edit form selected values

Struggling with an issue in Laravel 4, in a "contact" model edit form, I can get all fields current values except those from the multiple select which is to establish a relation with another model "company". It's a many-to-many relationship. I'm getting the list of companies, but none are selected even if a relation exists.

Here's my edit form:

{{ Form::model($contact, array('route' => array('crm.contacts.update', $contact->id), 'id' => 'edit-contact')) }}
        <div class="control-group">
            {{ Form::label('first_name', 'First Name', array( 'class' => 'control-label' )) }}
            {{ Form::text('first_name') }}
        </div>
        <div class="control-group">
            {{ Form::label('last_name', 'Last Name', array( 'class' => 'control-label' )) }}
            {{ Form::text('last_name') }}
        </div>
        <div class="control-group">
            {{ Form::label('email', 'Company Email', array( 'class' => 'control-label' )) }}
            {{ Form::text('email') }}
        </div>
        <div class="control-group">
            {{ Form::label('company_ids', 'Company', array( 'class' => 'control-label' )) }}
            {{ Form::select('company_ids[]', $companies, array('',''), array('multiple'), Input::old('company_ids[]')) }}
        </div>
{{ Form::close() }}

My controller:

public function edit($id)
{
    $contact = Contact::find($id);
    $company_options = Company::lists('name', 'id');
    return View::make('crm.contacts.edit')
        ->with('contact', $contact)
        ->with('companies', $company_options);;
}

Any ideas on how to have the multiple select field pre-filled with existing values?

Thanks

Upvotes: 6

Views: 11042

Answers (2)

user2115377
user2115377

Reputation:

Laravel does support multiselect by default.

{{ Form::select('members[]', $users, null, array('multiple' => true)); }}

Upvotes: 10

Ryun
Ryun

Reputation: 729

Laravel does not support multi-select fields by default you need to use a Form::macro

The example below by @Itrulia was correct, you can simply do:

$users = array(
    1 => 'joe',
    2 => 'bob',
    3 => 'john',
    4 => 'doe'
);
echo Form::select('members[]', $users, array(1,2), array('multiple' => true));

Upvotes: 12

Related Questions