One of the requirements in an application that I am building is for a form input which takes in a varying number of items for a single field. For instance, sports that I play are ('Soccer','Tennis','Croquet').
There are a finite number of sports one can play (arguably), so these items should be selected from a "drop down" type list in the form input.
Downstream of this form will be two tables which have a one-to-many relationship. So from the above, the "user" table would have a single row, while the "user_sports" table would have three rows. These would then be linked by the id field in the user table.
I have not been able to find the sort of functionality where this can be achieved in the documentation (perhaps I am not searching for the correct thing). Below was the closest that I found, but is only for selecting a single item from a drop down list.
http://laravel.com/docs/html#drop-down-lists
Is there a workaround out there that will enable me to get this form element up and running using the Laravel framework?
Alternatively, are there other ways that this sort of functionality can be achieved, without damaging the user experience?
I agree with user3158900, and I only differ slightly in the way I use it:
However, in my experience the 3rd parameter of the select is a string only, so for repopulating data for a multi-select I have had to do something like this:
@SamMonk your technique is great. But you can use laravel form helper to do so. I have a customer and dogs relationship.
On your controller
On customer create view you can use.
Third parameter accepts a list of array a well. If you define a relationship on your model you can do this:
Update For Laravel 5.1
The lists method now returns a Collection. Upgrading To 5.1.0
Laravel 4.2
@SamMonk gave the best alternative, I followed his example and build the final piece of code
In my project I'm going to have many table relationships like this so I wrote an extension to keep it clean. To load it, put it in some configuration file like "app/start/global.php". I've created a file "macros.php" under "app/" directory and included it in the EOF of global.php
Usage
List without pre-selected items (create view)
Preselections (edit view)
Complete usage
A multiple select is really just a select with a
multiple
attribute. With that in mind, it should be as easy as...The first parameter is just the name, but post-fixing it with the
[]
will return it as an array when you useInput::get('sports')
.The second parameter is an array of selectable options.
The third parameter is an array of options you want pre-selected.
The fourth parameter is actually setting this up as a multiple select dropdown by adding the
multiple
property to the actual select element..Just single if conditions
My solution, it´s make with jquery-chosen and bootstrap, the id is for jquery chosen, tested and working, I had problems concatenating @foreach but now work with a double @foreach and double @if:
this is the code por jquery chosen (the blade.php code doesn´t need this code to work)