title: querying based on the additional pivot table column in laravel
body: <p>I have a User Model with the columns id, name, email and</p>
  
  <p>a Course Model with the columns id, name, code and</p>
  
  <p>a pivot table course_user.</p>
  
  <p>Course and User has many-to-many relation</p>
  
  <p>course_user table contains columns course_id, user_id, section</p>
  
  <p>While taking courses, some users will be listed as 'Pending' in pivots section column and some users will be listed as 'A', 'B' etc in section column.</p>
  
  <p><strong>Question:</strong> I want to get those courses which are taken by at least one user and section is 'Pending'
  So far, I've tried these: 
  In Course Model : </p>
  
  <pre><code>public function users(){
  
      return $this-&gt;belongsToMany(User::class)
                  -&gt;withPivot('section');
                  -&gt;wherePivot('section', 'Pending');
  }
  </code></pre>
  
  <p>Another Approach I've tried in course Model:</p>
  
  <pre><code>public function pendingUsers(){
  
      return $this-&gt;belongsToMany(User::class)
                  -&gt;withPivot('section')
                  -&gt;wherePivot('section', 'like', 'Pending');
  }
  </code></pre>
  
  <p>But all of these are giving me all of the courses.
  Now, How can this job be done?</p>