title: Laravel - Get data from Many to Many relationship
body: <p>I am building a small application in order to experiment with many-to-many relationships in Laravel 7. This application is about projects and users (many-to-many).</p>
  <p>An authenticated user can create new projects. Each project has its own page. On the page of a project, a list is rendered, with the name of each user who is a member to this project, and next to each name, is a button that should remove the specific user/member from that project.</p>
  <p>Right now, I'm stuck at the &quot;remove user&quot; function. I do not understand how to get the id of each user on the list, and pass it to the function that will &quot;detach&quot; the many-to-many relationship.</p>
  <p>My files are below:</p>
  <p><strong>Migrations</strong>:</p>
  <p>Users_Table:</p>
  <pre><code>public function up()
  {
      Schema::create('users', function (Blueprint $table) {
          $table-&gt;bigIncrements('id');
          $table-&gt;boolean('is_admin')-&gt;nullable();
          $table-&gt;string('name');
          $table-&gt;string('email')-&gt;unique();
          $table-&gt;timestamp('email_verified_at')-&gt;nullable();
          $table-&gt;string('password');
          $table-&gt;rememberToken();
          $table-&gt;timestamps();
      });
  }
  </code></pre>
  <p>Projects_Table:</p>
  <pre><code>public function up()
  {
      Schema::create('projects', function (Blueprint $table) {
          //primary key
          $table-&gt;bigIncrements('id');
  
          //foreign keys columns
          $table-&gt;unsignedBigInteger('user_id');
  
          //normal table columns
          $table-&gt;string('title')-&gt;unique();
  
          //created_at and updated_at columns
          $table-&gt;timestamps();
  
          //add constraints
          $table-&gt;foreign('user_id')
          -&gt;references('id')
          -&gt;on('users')
          -&gt;onDelete('cascade');
      });
  
  }
  </code></pre>
  <p>Project_Users_Table (Pivot):</p>
  <pre><code>public function up()
  {
      Schema::create('project_user', function (Blueprint $table) {
          $table-&gt;foreignId('project_id')-&gt;constrained();
          $table-&gt;foreignId('user_id')-&gt;constrained();
          $table-&gt;timestamps();
      });
  }
  </code></pre>
  <p><strong>Models</strong>:</p>
  <p>User.php:</p>
  <pre><code>public function projects()
  {
      return $this-&gt;belongsToMany('App\Project');
  }
  </code></pre>
  <p>Project.php:</p>
  <pre><code>protected $fillable = ['name'];
  
  public function members()
  {
      return $this-&gt;belongsToMany('App\User');
  }
  
  public function creator()
  {
      return $this-&gt;belongsTo('App\User', 'user_id', 'id');
  }
  </code></pre>
  <p><strong>View</strong>:</p>
  <pre><code>&lt;ul&gt;
      @foreach($project-&gt;members as $member)
      &lt;li&gt;{{$member-&gt;name}}
  
          &lt;!-- if connected user is admin, show &quot;remove user&quot;  button --&gt;
          @if ( Auth::user()-&gt;is_admin == 1 )
          &lt;form class=&quot;delete_project_form&quot; method=&quot;POST&quot; action=&quot;{{url('/projects/'.$project-&gt;id.'/remove_user/'.$member-&gt;id)}}&quot;&gt;
              @csrf
              @method('DELETE')
              &lt;button type=&quot;submit&quot; value=&quot;Delete&quot;&gt;Remove&lt;/button&gt;
          &lt;/form&gt;
          @endif
  
      &lt;/li&gt;
      @endforeach
  &lt;/ul&gt;
  
   
  </code></pre>
  <p><strong>Route</strong>:</p>
  <p>web.php:</p>
  <pre><code>Route::delete('/projects/{project}/remove_user/{member}', 'ProjectController@remove_user');
  </code></pre>
  <p><strong>Controller</strong>:</p>
  <p>ProjectController.php:</p>
  <pre><code>public function remove_user(Project $project)
  {
      $user_id = $project-&gt;members()-&gt;id;
      $project-&gt;members()-&gt;detach($user_id);
      return redirect('/projects/'.$project-&gt;id);
  }
  </code></pre>
  <p>I know it obviously is not correct, but with this configuration it gives the following error:</p>
  <blockquote>
  <p>Undefined property:
  Illuminate\Database\Eloquent\Relations\BelongsToMany::$id</p>
  </blockquote>
  <blockquote>
  <p>http://localhost/rel_test/public/projects/6/remove_user/2</p>
  </blockquote>
  <p>Can you guys help ? Thank you in advance</p>