title: Laravel Eloquent Many to Many relationship using a pivot table
body: <p>I'm working on creating my own Project Manager, I currently have a <code>projects</code>, <code>users</code> and a <code>project_users</code> table. I am trying to create the relationships in the models to associate one project to many users, but I have never created a many to many relationship. I looked at the documentation and tried various examples but none worked for me.</p>
  
  <p>Here is my ProjectController:</p>
  
  <p>public function single( $id ) {</p>
  
  <pre><code>$project = Project::with('client','projecttypes','storys', 'sprints', 'projectusers')-&gt;find( $id )-&gt;toArray();
  
  return View::make('projects.single', array( 'project' =&gt; $project ) );
  </code></pre>
  
  <p>} </p>
  
  <p>This is simply grabbing a single project and all of the related models and returning them to the view.</p>
  
  <p>My Project model:</p>
  
  <pre><code>class Project extends Eloquent {
  
      /**
       * The database table used by the model.
       *
       * @var string
       */
      protected $table = 'projects';
  
      protected $fillable = array( 'client_id', 'title', 'description', 'type_id', 'status', 'hours_estimated', 'hours_actual', 'date_estimated', 'date_due', 'date_completed', 'created_at', 'updated_at' );
  
      public function projectusers() {
          return $this -&gt; hasMany( 'User', 'id', 'user_id' );
      }
  
  }
  </code></pre>
  
  <p>My users model:</p>
  
  <pre><code>use Illuminate\Auth\UserTrait;
  use Illuminate\Auth\UserInterface;
  use Illuminate\Auth\Reminders\RemindableTrait;
  use Illuminate\Auth\Reminders\RemindableInterface;
  
  class User extends Eloquent implements UserInterface, RemindableInterface {
  
      use UserTrait, RemindableTrait;
  
      /**
       * The database table used by the model.
       *
       * @var string
       */
      protected $table = 'users';
  
      /**
       * The attributes excluded from the model's JSON form.
       *
       * @var array
       */
      protected $hidden = array( 'password', 'remember_token' );
  
      protected $fillable = array( 'client_id', 'title', 'first_name', 'last_name', 'address', 'city', 'state', 'zip', 'email', 'password', 'status', 'created_at', 'updated_at', 'remember_token', '_token' );
  
      public function project() {
          return $this -&gt; belongsToMany('Project');
      }
  
  }
  </code></pre>
  
  <p>and finally my ProjectUsers model:</p>
  
  <pre><code>class ProjectUsers extends Eloquent {
  
      /**
       * The database table used by the model.
       *
       * @var string
       */
      protected $table = 'project_users';
  
      protected $fillable = array( 'project_id', 'assigner_id', 'user_id', 'notifications', 'created_at', 'updated_at' );
  
      public function project() {
          $this -&gt; belongsTo('Project');
      }
  
  }
  </code></pre>
  
  <p>All fields in the database are listed in the models <code>$fillable</code> with the exception of the primary id <code>id</code>.</p>
  
  <p>I just need to figure out how to get all users assigned to a specific project utilizing the pivot table, any help would be greatly appreciated!</p>
  
  <p>Thanks</p>