3

On my edit page I'm trying to make my checkbox auto checked based on the permissions that I already assigned to my roles, I don't know how to make it work. Any idea? Currently with my code I'm getting unchecked checkbox with the list of all my permissions.

Edit.blade

var app = new Vue({
  el: '#app',
  data: {
    permissionsSelected: []
  }
});
<div class="from-group">
  <div class="checkbox-group" v-model="permissionsSelected">
    @foreach ($permissions as $permission)
    <div class="field">
      <input type="checkbox" value="{{$permission->id}}" name="permissions[]">{{$permission->display_name}}
    </div>
    @endforeach

  </div>

2
  • Check the "multiple checkboxes" part: vuejs.org/v2/guide/forms.html#Checkbox You would need to change your markup a bit, and in order to auto check, to populate the permissionsSelected array with the appropiate checkbox values. Commented Dec 29, 2017 at 17:48
  • bro. my checkbox value is i'm getting it on my roles which is stored in my database how to pull it out? Commented Dec 29, 2017 at 17:55

1 Answer 1

1

Remove v-model="permissionsSelected" from div and add it to each checkbox:

<div class="from-group">
  <div class="checkbox-group">
    @foreach ($permissions as $permission)
    <div class="field">
      <input
        type="checkbox" 
        value="{{$permission->id}}"
        name="permissions[]" 
        v-model="permissionsSelected"> {{$permission->display_name}}
    </div>
    @endforeach
</div>

Also make sure your data setup is a function returning an object:

data () {
  return {
    permissionsSelected: []
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

how to return my data on the permissionSelected since my data is base on my database how to pull it out ? meaning permission_id +user_id?
Selected permissions will populate permissionsSelected array. You don't need to do anything for this to work.
var app = new Vue({ el: '#app', data () { return { permissionsSelected: [] } } }); not working bro.

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.