title: Laravel fetch only pivot columns in many to many relationship
body: <p>I have a <code>User</code> model that relates to a <code>Section</code> model through a pivot model <code>UserTrainingSection</code>. I have a pivot table that stores the foreign keys for both tables called <code>section_user</code>.</p>
  
  <p>I have 2 additional pivot table columns called <code>completed</code> and <code>completed_date</code>.</p>
  
  <p>The problem I am having is that when I fetch my data it returns all the columns from the <code>User</code> model along with the additional pivot columns.</p>
  
  <pre><code>    class Section extends Model
      {
          public $table = "sections";
  
          public $fillable = [
              'id',
              'name',
              'description',
              'parent',
              'position',
              'completion_percentage'
          ];
  
          public function users()
          {
              return $this-&gt;belongsToMany(User::class, 'section_user')-&gt;using('App\Models\UserTrainingSection')-&gt;withPivot('completed', 'completed_date');
          }
      }
  </code></pre>
  
  <p>In my API service I fetch my data like this:</p>
  
  <pre><code>Section::with(['users' =&gt; function ($q) {
                  $q-&gt;where('users.id', Auth::user()-&gt;id);
              }])-&gt;first();
  </code></pre>
  
  <p>How do I only return the pivot table columns and exclude the columns from the user table?</p>
  
  <p>At the moment it returns something like this:</p>
  
  <pre><code>"sections": [
                  {
                      "id": 2,
                      "name": "Subsection 1 training",
                      "description": null,
                      "parent": 1,
                      "position": 2,
                      "completion_percentage": null,
                      "created_at": "2018-05-04 09:54:09",
                      "updated_at": "2018-05-11 09:14:59",
                      "users": [
                          {
                              "id": 1,
                              "name": "Test",
                              "email": "test@test.com",
                              "created_at": "12-04-2018 14:51:42",
                              "updated_at": "2018-04-19 14:14:36",
                              "pivot": {
                                  "section_id": 2,
                                  "user_id": 1,
                                  "completed": 1,
                                  "completed_date": "31/05/2018",
                                  "expires": "31/05/2019"
                              },
                          }
                      ]
                  }
              ]
  </code></pre>
  
  <p>What I would like to return is something like this:</p>
  
  <pre><code>"sections": [
                      {
                          "id": 2,
                          "name": "Subsection 1 training",
                          "description": null,
                          "parent": 1,
                          "position": 2,
                          "completion_percentage": null,
                          "created_at": "2018-05-04 09:54:09",
                          "updated_at": "2018-05-11 09:14:59",
                          "users": [
                              {
                                  "pivot": {
                                      "section_id": 2,
                                      "user_id": 1,
                                      "completed": 1,
                                      "completed_date": "31/05/2018",
                                      "expires": "31/05/2019"
                                  }
                              }
                          ]
                      }
                  ]
  </code></pre>
  
  <p>So I basically get rid of the user data I don't want and only return the pivot data.</p>