title: Laravel: Get users based on Eloquent relation
body: <p>I have a <code>User</code> model which has an attribute <code>type</code> among other attributes. Type is used to identify <em>parents</em> and <em>children</em>.</p>
  
  <p>Parent and children (students) have <em>many-to-many</em> relationship.
  Also students belong to one or many <em>groups</em> (model <code>Group</code>).</p>
  
  <p>User model</p>
  
  <pre><code>/**
   * Filter the scope of users to student type.
   *
   * @param $query
   */
  public function scopeStudent($query){
      $query-&gt;where('type', '=', 'std');
  }
  
  /**
   * Filter the scope of users to parent type.
   *
   * @param $query
   */
  public function scopeParent($query){
      $query-&gt;where('type', '=', 'prt');
  }
  
  /**
   * List of groups the user is associated with.
   *
   * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
   */
  public function groups(){
      return $this-&gt;belongsToMany('\App\Group', 'group_user_assoc')
                  -&gt;withTimestamps();
  }
  
  /**
   * List of students associated with the user(parent).
   *
   * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
   */
  public function children(){
      return $this-&gt;belongsToMany('\App\User', 'student_parent_assoc', 'parent_id', 'student_id')
                  -&gt;withPivot('relation');
  }
  
  /**
   * List of parents associated with the user(student).
   *
   * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
   */
  public function parents(){
      return $this-&gt;BelongsToMany('\App\User', 'student_parent_assoc', 'student_id', 'parent_id')
                  -&gt;withPivot('relation');
  }
  </code></pre>
  
  <p>The aforementioned relations are working correctly.</p>
  
  <p>Below are my association tables.</p>
  
  <pre><code>student_parent_assoc
  ----------------------
  +------------+------------------+------+-----+
  | Field      | Type             | Null | Key |
  +------------+------------------+------+-----+
  | student_id | int(10) unsigned | NO   | PRI |
  | parent_id  | int(10) unsigned | NO   | PRI |
  | relation   | varchar(25)      | YES  |     |
  +------------+------------------+------+-----+
  
  group_user_assoc
  ----------------------
  +------------+------------------+------+-----+
  | Field      | Type             | Null | Key |
  +------------+------------------+------+-----+
  | group_id   | int(10) unsigned | NO   | MUL |
  | user_id    | int(10) unsigned | NO   | MUL |
  | created_at | timestamp        | NO   |     |
  | updated_at | timestamp        | NO   |     |
  +------------+------------------+------+-----+
  </code></pre>
  
  <p>I need to find students who do not belong to any group along with their parents. I have managed to find students like so</p>
  
  <pre><code>$students = User::student()
                  -&gt;whereDoesntHave('groups')
                  -&gt;get();
  </code></pre>
  
  <p><strong>Question:</strong>
  Now I want to find parents of these students. But I am not able to build an eloquent query for the same. How do I do it?</p>
  
  <p>Note: I could get collection of students from above query and run a <code>foreach</code> loop to get their parents like so</p>
  
  <pre><code>$parents = new Collection;
  foreach($students as $student) {
      foreach($student-&gt;parents as $parent) {
          $parents-&gt;push($parent);
      }
  }
  $parents = $parents-&gt;unique();
  </code></pre>
  
  <p>But I need a <code>Builder</code> object and not a <code>Collection</code> as I am using <a href="https://datatables.net/examples/server_side/" rel="nofollow">Datatables server side processing</a> with <a href="https://packagist.org/packages/yajra/laravel-datatables-oracle" rel="nofollow">Yajra/datatables</a>.</p>