title: laravel belongsToMany with all rows of model
body: <p>I have a belongsToMany relation of user and resource.</p>
  
  <p><strong>User:</strong></p>
  
  <pre><code>    public function resources() {
          return $this-&gt;belongsToMany('Resource')-&gt;withPivot(array('value'));
      }
  </code></pre>
  
  <p><strong>Resource:</strong></p>
  
  <pre><code>    public function users() {
          return $this-&gt;belongsToMany('User')-&gt;withPivot(array('value'));
      }
  </code></pre>
  
  <p>the intermediate table is <code>resource_user</code>.
  With:</p>
  
  <pre><code>$resources = Sentry::getUser()-&gt;resources()-&gt;get();
  </code></pre>
  
  <p>I get all the resources where the user_id is in the resource_user table.
  so far so good.
  But how can I get ALL the resources entries, even the user_id is not present at the intermediate table?
  Let's say I have 3 resources in the resource Table. Id 1, 2 and 3.
  I have one user with ID 1. In the intermediate Table I have ONLY 2 rows:</p>
  
  <blockquote>
    <p>resource_id 1 and user_id 1 and value 50.</p>
    
    <p>resource_id 2 and user_id 1 and value 100.</p>
  </blockquote>
  
  <p>But I want display ALL the resources, and if the user is not present there, there should be resource object but without the relation model of the user.</p>
  
  <p><code>whereHas</code> isn't working with this problem. </p>
  
  <p>so my goal is to get 3 results, and not 2.
  something like this:</p>
  
  <blockquote>
    <p>resource_id 1 user_id 1 value 50</p>
    
    <p>resource_id 2 user_id 1 value 100</p>
    
    <p>resource_id 3 user_id 0 value 0</p>
  </blockquote>
  
  <p>The only thing what I figured out is this:</p>
  
  <p>In the resource model I create a function like this:</p>
  
  <pre><code>    # Get specific User value from resource
      public function userValue() {
          $res_user = DB::table('resource_user')-&gt;whereUserId(Sentry::getUser()-&gt;id)-&gt;whereResourceId($this-&gt;id)-&gt;first();
      if($res_user){
          return $res_user-&gt;value;
      }else{
          return 0;
      }
  }
  </code></pre>
  
  <p>But I wonder there exist an cleaner eloquent way for this?
  Any Idea of that?</p>