title: Laravel 5 pivot table project<->user
body: <p>I find the pivot tables pretty complicated and I don't see what to do next or what I am doing wrong, I've found some tutorials but didn't help me in my needs.</p>
  
  <p>I have a <code>projects</code> and <code>users</code> with a <code>many-to-many</code> relation.
  One <code>project hasMany users</code> and One <code>user hasMany projects</code>.</p>
  
  <p>What I have now leaves projects without a relationship to a user.</p>
  
  <p>This is what I have so far:</p>
  
  <p><strong>Projects table</strong></p>
  
  <pre><code>class CreateProjectsTable extends Migration {
  
  public function up()
  {
      Schema::create('projects', function(Blueprint $table)
      {
          $table-&gt;increments('id');
          $table-&gt;string('name');
          $table-&gt;date('completion_date');
          $table-&gt;integer('completed')-&gt;default(0);
          $table-&gt;integer('active')-&gt;default(0);
          $table-&gt;timestamps();
      });
  }
  </code></pre>
  
  <p><strong>Users table</strong></p>
  
  <pre><code>class CreateUsersTable extends Migration {
  
  public function up()
  {
      Schema::create('users', function(Blueprint $table)
      {
          $table-&gt;increments('id');
          $table-&gt;integer('company_id');
          $table-&gt;integer('project_id');
          $table-&gt;integer('usertype_id')-&gt;default(0);
          $table-&gt;string('username');
          $table-&gt;string('password');
      });
  }
  </code></pre>
  
  <p><strong>Project User table (pivot)</strong></p>
  
  <pre><code>class CreateProjectUsersTable extends Migration {
  
  public function up()
  {
      Schema::create('project_users', function(Blueprint $table)
      {
          $table-&gt;increments('id');
          $table-&gt;integer('project_id')-&gt;references('id')-&gt;on('project');;
          $table-&gt;integer('user_id')-&gt;references('id')-&gt;on('user');;
      });
  }
  </code></pre>
  
  <p><strong>User model</strong></p>
  
  <pre><code>public function projects() {
      return $this-&gt;belongsToMany('App\Project', 'project_users', 'user_id', 'project_id');
  }
  </code></pre>
  
  <p><strong>Project model</strong></p>
  
  <pre><code>public function users() {
      return $this-&gt;belongsToMany('App\User', 'project_users', 'project_id', 'user_id');
  }
  </code></pre>
  
  <p><strong>Project controller</strong></p>
  
  <pre><code>public function index(Project $project)
  {
  
     $projects = $project-&gt;with('users')-&gt;get();
  
      dd($projects);
  
      $currenttime = Carbon::now();
  
      //return view('project.index', array('projects' =&gt; $projects, 'currenttime' =&gt; $currenttime));
  
      return view('user.index', compact('projects'));
  }
  </code></pre>