title: How to get 3 items from the pivot table via Eloquent (Laravel)?
body: <p>A bit of business logic: 1 company may have many users with different roles. 1 user may have only 1 role in company.</p>
  
  <p>In the database I have a pivot table <code>company_user_roles</code>. There're 3 fields: <code>company_id, user_id, role_id</code>. Then I have a <code>User</code> model and <code>belongsToMany</code> relation for getting user's companies:</p>
  
  <pre><code>public function companies()
      {
          return $this-&gt;belongsToMany(
           'App\Company', 
           'company_user_roles', 
           'user_id', 
           'company_id'
          );
      }
  </code></pre>
  
  <p><code>$user-&gt;companies</code> returns a collection of companies. But there's no user's role in each company. I have only this for each company (and it seems to be expected): </p>
  
  <pre><code>#attributes: array:4 [▼
          "id" =&gt; 1
          "name" =&gt; "Company name"
          "created_at" =&gt; "2018-07-08 15:45:40"
          "updated_at" =&gt; "2018-07-08 15:45:42"
  ]
  </code></pre>
  
  <p>But I don't understand how to get a role of the user for each company. I want to get something like this: </p>
  
  <pre><code>#attributes: array:4 [▼
              "id" =&gt; 1
              "name" =&gt; "Company name"
              "role" =&gt; [
                "id": 1,
                "name": "owner"
              ]
              "created_at" =&gt; "2018-07-08 15:45:40"
              "updated_at" =&gt; "2018-07-08 15:45:42"
      ]
  </code></pre>
  
  <p>Or something like this, but without extra queries to the database. How to implement this logic in this case?</p>