简体   繁体   中英

Laravel 5.1 and illuminate html FORM Multiple select

I have a problem with multiple select in laravel using illuminate html, I want to pre-select items, it works when I put them manuelly with an array like this :

{!! Form::select('tags_list[]', $tags, [2, 3], ['class' => 'form-control', 'multiple' => true]) !!}

But when I use a model to put the list, like this :

{!! Form::select('tags_list[]', $tags, $post->tags_list, ['class' => 'form-control', 'multiple' => true]) !!}

It doesn't work and I checked the list is returned correctly, here is the result of :

dd($post->tags_list);

Collection {#273 ▼
  #items: array:2 [▼
    0 => 1
    1 => 2
  ]
}

And here is the method that return the tags_list

public function getTagsListAttribute()
    {
        return $this->tags->lists('id');
    }

I am new in Laravel framework, can anyone help me with this issue, thanks in advance

      <div class="form-group">
                {!! Form::labelControl('user_type','User Type'),['class'=>'control-label col-md-2'], TRUE )  !!}

                <div class="col-md-3">
                    {!! Form::select('user_type[]', $selected_users, null, ['id'=>'user_type','multiple'=>'multiple','multiselect'=>'multiselect','class'=>'form-control']) !!}
                </div>

      </div>



       <script>
                    $(document).ready(function () {
                        $("#user_type").multiselect();
                   });
       </script>

在此处输入图片说明

Things to remember:

  • user_type is the array that contains the type of users like : Teacher , Tutor , Admin etc...
  • This name should be same as the table in your db(in my case table name is user_type)
  • $selected_users is the array that contains the id of the users that need to be pre selected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM