title: How To Populate Checkbox dynamically using Laravel Eloquent
body: <p>I have <strong>user</strong> and <strong>role</strong> two table data,I've fetch the data using eloquent and send to my view.
  Now, I want to populate user role checkbox dynamically. at this time which is I've written as <strong>hard code</strong></p>
  
  <p>Here,is my code.</p>
  
  <pre><code>@foreach($users as $user)
      &lt;tr&gt;
          &lt;td&gt;{{$user-&gt;name}}&lt;/td&gt;
          &lt;td&gt;{{$user-&gt;email}}&lt;/td&gt;
  
           @foreach($user-&gt;roles as $role)
  
          &lt;td&gt;&lt;input type="checkbox" name="" {{$user-&gt;hasRole('User') ? 'checked' : ''}}&gt;&lt;/td&gt;
          &lt;td&gt;&lt;input type="checkbox" name="" {{$user-&gt;hasRole('Admin') ? 'checked' : ''}}&gt;&lt;/td&gt;
          &lt;td&gt;&lt;input type="checkbox" name="" {{$user-&gt;hasRole('Author') ? 'checked' : ''}}&gt;&lt;/td&gt;
           @endforeach
          &lt;td&gt;&lt;/td&gt;
      &lt;/tr&gt;
      @endforeach
  </code></pre>
  
  <p>My Eloquent Query.</p>
  
  <pre><code>    $users=User::with('roles')-&gt;get();
  
      return view('admin',compact('users'));
  </code></pre>
  
  <p>User Model Relationship.</p>
  
  <pre><code> public function roles()
  {
      return $this-&gt;belongsToMany('App\Role');
  }
  </code></pre>