title: Laravel Eager Loading for relationship model - unable to reduce queries unless i return null
body: <p>I am trying to make reduce my database queries when retrieving from the database using Eloquent.</p>
  
  <p>I'm trying to take advantage of Eager Loading to reduce the queries and not get the N+1 issue.. I've got some relationships in my models too.</p>
  
  <p>I have noticed (and I'm not sure if this is a red herring) that if I just return null from my controller method then I get 6 queries. Which would indicate Eager Loading working. But I get 492 queries when I return the result of the eloquent variable. EG. In my controller:</p>
  
  <pre><code>    public function getJobItems()
      {
  
          $userId = auth()-&gt;user()-&gt;id;
  
  
          $jobs = JobItem::with(['project', 'user'])
              -&gt;whereHas('project', function ($query) use ($userId) {
                  $query-&gt;where('user_id', '=', $userId);
              })
              -&gt;get();
  
          return $jobs;
  
      }
  </code></pre>
  
  <p>If I change return $jobs; to just return; then I can see only 6 queries (shown with Laravel Telescope). I'm not sure how outputting the $jobs var changes the queries made to the DB. Here is what I get from Telescope query inspector for those 6 queries:</p>
  
  <pre><code>update `sessions` set `payload` =...        
  select `users`.*, `projects`.`user_id` as `laravel_through_key` from `users` inner join `projects` on...    
  select * from `projects` where `projects`.`id` in (5, 9, 15, 111, 116, 117, 118, 119, 123, 124) and...  
  select * from `job_items` where exists (select * from `projects` where `job_items`.`project_id` =...    
  select * from `users` where `id` = 1 and `users`.`deleted_at`
  select * from `sessions` where `id` = 'acx7zPzeeRVrtI5FTUGUbULm1bEVQ0T5AVylFIf6'
  </code></pre>
  
  <p>When I use return $jobs; I get 492 queries and telescope explains 484 are duplicates. For instance variations on:</p>
  
  <pre><code>select * from `projects` where `projects`.`id` = 15 and `projects`.`deleted_at` is null limit 1
  </code></pre>
  
  <p>In my JobItem Model I have the following:</p>
  
  <pre><code>
      public function project()
      {
          return $this-&gt;belongsTo('App\Project', 'project_id');
      }
  
  
      public function user()
      {
  
          return $this-&gt;hasOneThrough(
              User::class,
              Project::class,
              'user_id',
              'id',
              'project_id',
              'id'
          );
      }
  </code></pre>
  
  <p>I have the following tables:</p>
  
  <pre><code>table: job_items
  columns: id, project_id
  
  table: projects
  columns: id, user_id
  
  table: users
  column: id
  
  </code></pre>
  
  <p>I want to output all users connected to JobItems</p>
  
  <p>In my JobItem.php model I have tried</p>
  
  <pre><code>class JobItem extends Model
  {
  
      public function user()
      {
  
  
          return $this-&gt;hasOneThrough(
              Project::class,
              User::class,
              'project_id',
              'id',
              'belongs_to',
              'user_id'
          );
  
      }
  }
  
  </code></pre>
  
  <p>Any ideas on how to reduce my queries with these models would be appreciated. Thanks</p>