title: Write to hasMany relationship with Eloquent ORM/Laravel
body: <p>Question about Laravel's Eloquent ORM. I've had a look on SO already, apologies if I've missed a similar question.</p>
  
  <p>I have a User model, and I'm trying to write an array of Permissions back to the UserPermissions model, via the relationship in the User model.</p>
  
  <p>I've setup the hasMany link already, and can retrieve the correct data from the User object (<code>$user-&gt;roles</code>), but can't work out how to write back if it is at all possible.</p>
  
  <p>This is my basic user model:</p>
  
  <pre><code>class User extends Eloquent {
  
      protected $table = 'users';
  
      public $timestamps = false;
  
      protected function roles() {
  
          return $this-&gt;hasMany('UserPermission');
  
      }
  
  }
  </code></pre>
  
  <p>and the UserPermission model:</p>
  
  <pre><code>class UserPermission extends Eloquent {
  
      protected $table = 'users_permissions';
  
      public $timestamps = false;
  
      protected $fillable = ['role_id', 'user_id'];
  
  }
  </code></pre>
  
  <p>At this stage, I've reverted to saving the User, and then running <code>UserPermission::insert()</code>, which allows an array to be passed, but I'd prefer to do it in one step if possible.</p>
  
  <p>Anyone know if this is possible?</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>Table structure is below for your perusal:</p>
  
  <p>users table</p>
  
  <pre><code>+------------------+------------------+------+-----+---------+----------------+
  | Field            | Type             | Null | Key | Default | Extra          |
  +------------------+------------------+------+-----+---------+----------------+
  | id               | int(10) unsigned | NO   | PRI | 0       | auto_increment |
  | username         | varchar(255)     | NO   |     |         |                |
  +------------------+------------------+------+-----+---------+----------------+
  </code></pre>
  
  <p>users_roles table</p>
  
  <pre><code>+------------------+------------------+------+-----+---------+----------------+
  | Field            | Type             | Null | Key | Default | Extra          |
  +------------------+------------------+------+-----+---------+----------------+
  | id               | int(10) unsigned | NO   | PRI | 0       | auto_increment |
  | name             | varchar(255)     | NO   |     |         |                |
  | description      | varchar(255)     | YES  |     | NULL    |                |
  | url_access       | varchar(255)     | NO   |     |         |                |
  +------------------+------------------+------+-----+---------+----------------+
  </code></pre>
  
  <p>users_permissions table</p>
  
  <pre><code>+------------------+------------------+------+-----+---------+----------------+
  | Field            | Type             | Null | Key | Default | Extra          |
  +------------------+------------------+------+-----+---------+----------------+
  | id               | int(10) unsigned | NO   | PRI | 0       | auto_increment |
  | role_id          | int(10) unsigned | NO   |     |         |                |
  | user_id          | int(10) unsigned | NO   |     |         |                |
  +------------------+------------------+------+-----+---------+----------------+
  </code></pre>
  
  <p><code>role_id</code> relates to <code>users_roles.id</code> and <code>user_id</code> relates to <code>users.id</code></p>
  
  <p>As background logic, the <code>users_roles</code> table has a <code>url_access</code> column which contains a url. If the user is assigned to that role, they have access to the subpages from that url.</p>