Multiple selection of the editing form for selected values

Struggling with the problem in Laravel 4, in the form of editing the contact model, I can get all the current values โ€‹โ€‹of the fields, except those indicated in the multiple choice, which should establish a connection with another model of "company". This is a many-to-many relationship. I get a list of companies, but no one gets selected, even if there is a relationship.

Here is 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 a multiple select box pre-populated with existing values?

thanks

+6
source share
2 answers

Laravel does not support multiple default selection fields you need to use the :: macro form

The @Itrulia example below was right, you can just do:

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

Laravel by default supports multiselect.

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

Source: https://habr.com/ru/post/949919/


All Articles