title: How to checked the value in checkbox when doing edit form?
body: <p>I have edit modal window with checkbox input in my laravel project. I want the value in checkbox checked when I open the edit form. Idk why it always checked the first value and not suit with the value from database like this picture:</p>
  <p><a href="https://i.stack.imgur.com/XKnCO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XKnCO.png" alt="enter image description here" /></a></p>
  <p>HTML code:</p>
  <pre><code>&lt;form action=&quot;&quot; method=&quot;put&quot; id=&quot;editForm&quot;&gt;
      &lt;div id=&quot;editModal&quot;&gt;
          &lt;div class=&quot;form-group row&quot;&gt;
              &lt;label for=&quot;roles&quot; class=&quot;col-md-2 col-form-label&quot;&gt;Roles&lt;/label&gt;
              &lt;div class=&quot;col-md-10 mt-2&quot;&gt;
                  &lt;?php foreach ($roles as $value) : ?&gt;
                      &lt;div class=&quot;form-check-inline&quot;&gt;
                          &lt;label class=&quot;form-check-label&quot;&gt;
                              &lt;input type=&quot;checkbox&quot; name=&quot;roles&quot; id=&quot;editRoles&quot; value=&quot;{{$value-&gt;id}}&quot;&gt;
                              &lt;?= $value-&gt;name ?&gt;
                          &lt;/label&gt;
                      &lt;/div&gt;
                  &lt;?php endforeach; ?&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  
  </code></pre>
  <p>Ajax script:</p>
  <pre><code>&lt;script&gt;
      $('body').on('click', '#edit', function(e) {
          id = $(this).data('id');
          $.ajax({
              url: &quot;users/&quot; + id + &quot;/edit&quot;,
              method: 'GET',
              success: function(result) {
                  $('#editModal').show();
                  for (let i = 0; i &lt; result.roles.length; i++) {
                      $('#editRoles').prop('checked', result.roles[i]);
                  }
                  console.log(result.roles);
              }
          });
      });
  &lt;/script&gt;
  </code></pre>
  <p>controller:</p>
  <pre><code>public function edit(Request $request, $id){
      $decode_id = Hashids::decodeHex($id);
      $role = Role::findOrFail($decode_id);
      $permission = $role-&gt;getPermissionNames();
  
      if ($request-&gt;ajax()) {
          return response()-&gt;json(['role' =&gt; $role, 'permission' =&gt; $permission]);
      }
  }
  </code></pre>
  <p>I also tried the solution from <a href="https://stackoverflow.com/questions/15044340/jquery-set-checkbox-checked">here</a> with this code:
  <code>$(&quot;#editModal input[type='checkbox']&quot;).prop('checked', result.roles[i])</code>;</p>
  <p>But it's checked all value! Anybody have solution for this?</p>