1
<?php
$arrayData = myModel::all();
foreach($arrayData as $arrayList){
   $pureData = array_add($pureData, $arrayList->elementValue, $arrayList->elementText);
}
echo Form::select('multiSelectName', $pureData, null, array());
?>
  1. I'm trying to get data from database to fill values into multiselect field.
  2. I'm trying to make some values selected, on this multiselect field.

How can I achieve that?

Thanks in advance.

2 Answers 2

3

One solution would be to get all available options and the already selected ones then find out if they match and create a select element with a selected parameter set to it.

I don't think this can be done using the Form helper (I know it can be done with just a single value, but have not tried it with multiple values)

Example

Get all the available options (try to get something to uniquely identify them like the id)

$all_options=Model::lists('id','name');

I'm using the lists method that will return me an array with all the available id's as the value and the name as the keys of the array.

Then get all the selected values

$already_selected_options =Model::with('related_model')->lists('id','name');

(If you are using eloquent something like this to get the related ids will work)

Then in your view

<select multiple>
@foreach($all_options as $name => $id)
    @if(in_array($id,$already_selected_options))
       <option value="{{$id}}" selected>{{$name}}</option>
      @else
       <option value="{{$id}}">{{$name}}</option>
    @endif
@endforeach
<select>
Sign up to request clarification or add additional context in comments.

5 Comments

I haven't done the exact same way, but you enlightened me through the solution. Thank you.
waste of cycles. my solution will work just the same, and leave the heavy lifting the client. I use it with the twig templating engine, albeit i program enterprise grade front ends. all server side costs must be spared at all times.
yes you are right, but what if the user has disabled javascript? It's not very often but it could happen
@HarryGeo who cares, its 2015. if they've disabled javascript, they are likely up to no good
well, that not exactly an excuse, the benefits you get from this is very small, you are just iterating 2 arrays it's not that bad for performance. My opinion is that compatibility is more important. Of course your answer is also right.
0

I'm of the opinion that you should do it in javascript instead if at all possible. It's barely noticeable in terms of latency, and doesn't waste cycles server side. Here's an example from one of my recent projects. as a bonus, if you declare an onchange event inline on the select, it will call it for you immediately following the execution of the function.

$('select.select-onload').each(function(index){
    if(typeof $(this).attr('data-selected') !== 'undefined'){
        if($(this).attr('data-selected') == ''){
            return false;
        }
        $(this).val( $(this).attr('data-selected') );
        //we may have a callback. lets manually invoke it.
        if(typeof $(this).attr('onchange') !== 'undefined'){
            var callback = $(this).attr('onchange').replace('();','').trim();
            window[callback].call(window);
        }
    }
});

usage:

<select class="select-onload" data-selected="{{id}}"> <!--whatever options value is 5 is the one that will be selected.--> </select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.