0

Hey so I'm a beginnerin Vue and I only encountered this problem when I used the foreach loop
Issue: Whenever I select any checkbox all get selected.

create.blade.php

  @foreach ($permissions as $permission)
              <li class="list-group-item bold">
                  <input type="checkbox" id="{{$permission->id}}"
                  :custom-value="{{$permission->id}}" v-model="permissionsSelected">
                  <label for="{{$permission->id}}">{{$permission->display_name}}
                  <em>{{$permission->description}}</em></label>
              </li>
            @endforeach
          </ul>
    <input type="hidden" name="permissions" :value="permissionsSelected">



<script>
 var app = new Vue({
   el: '#app',
   data: {
     permissionsSelected: ''
   }
 });
</script>
10
  • all checkboxes have same model v-model="permissionsSelected" that's why they all change together Commented Oct 20, 2017 at 6:17
  • Yes, that's how it's written in the docs as well: vuejs.org/v2/guide/forms.html Commented Oct 20, 2017 at 6:38
  • permissionsSelected should be an array, as written in the docs. Commented Oct 20, 2017 at 7:00
  • in your case it's a string, so change it topermissionsSelected: [] Commented Oct 20, 2017 at 7:11
  • also, you are missing value="" attribute on <input > Commented Oct 20, 2017 at 7:18

1 Answer 1

1

Your problems seems to be that <input> was missing :value="..". Also permissionsSelected should be an array: permissionsSelected: []

I would recommend you not to mix laravel blade @foreach with vue.

Instead pass $permissions to javascript and render the list with vue. You can look how to do that in this answer

One more thing, from laravel documentation

Since many JavaScript frameworks also use "curly" braces to indicate a given expression should be displayed in the browser, you may use the @ symbol to inform the Blade rendering engine an expression should remain untouched

And here is your updated example

<div id="app">
  <ul>
    <li class="list-group-item bold" v-for="permission in permissions">
      <input
        type="checkbox"
        :id="permission.id"
        :value="permission.id"
        v-model="permissionsSelected"
      >
      <label :for="permission.id">@{{ permission.display_name }}
        <em>@{{ permission.description }}</em>
      </label>
    </li>
  </ul>
  <input type="hidden" name="permissions" :value="permissionsSelected">
</div>

<script>
  var permissions = JSON.parse("{{ json_encode($permissions) }}");

  var app = new Vue({
    el: '#app',
    data: {
      permissions: permissions,
      permissionsSelected: [],
    }
  });
</script>
Sign up to request clarification or add additional context in comments.

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.