title: Relation not loading, when using select
body: <p><strong>Introduction</strong><br>
  Hello. I'm currently building a FacebookFeedParser, and at the moment I'm trying to build a method in a Controller that lists the Facebook pages with the best post/like ratio from today to the user. To do that, I have built the following query in pure SQL</p>
  
  <pre><code>SELECT pa.facebook_name, COUNT(po.id) AS postCount, SUM(likes) AS likes, SUM(likes)/COUNT(po.id) AS likesPerPost
  FROM facebook_posts po 
  INNER JOIN facebook_pages pa ON pa.id = po.facebook_page_id 
  WHERE CONVERT_TZ(`facebook_created_time`, 'UTC', 'Europe/Berlin') &gt; '2015-07-16 00:00:00' 
  GROUP by facebook_page_id 
  ORDER BY SUM(likes)/COUNT(po.id) DESC;
  </code></pre>
  
  <p>What I now wanted to do, is to transform this query into Laravel/Eloquent.</p>
  
  <p><strong>Current state</strong><br>
  I have the following classes in my project</p>
  
  <p><em>Controllers</em>: <code>PageController</code>, <code>PostController</code><br>
  <em>Models</em>: <code>FacebookPost</code>, <code>FacebookPage</code>, <code>BaseModel</code></p>
  
  <p>The <code>FacebookPost</code> and <code>FacebookPage</code> model are both defining their relations like this</p>
  
  <p><code>FacebookPage</code></p>
  
  <pre><code>/**
   * Defines the relation between FacebookPage and FacebookPost
   * One page can have multiple posts
   *
   * @see FacebookPage
   * @see FacebookPost
   *
   * @return \Illuminate\Database\Eloquent\Relations\HasMany
   */
  public function posts()
  {
      return $this-&gt;hasMany(FacebookPost::class);
  }
  </code></pre>
  
  <p><code>FacebookPost</code></p>
  
  <pre><code>/**
   * Defines the association of this object with FacebookPage
   * One facebook_post belongs to one facebook_page
   *
   * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
   */
  public function page()
  {
      return $this-&gt;belongsTo(FacebookPage::class, 'facebook_page_id');
  }
  </code></pre>
  
  <p>In the BaseModel, I have defined a scope, which will be used multiple times in the project</p>
  
  <pre><code>/**
   * Query scope to get the database values for today
   *
   * @param $query
   * @return mixed
   */
  public function scopeToday($query)
  {
      return $query-&gt;whereRaw('CONVERT_TZ(`'. $this-&gt;createDateColumn .'`, "UTC", "'. env('APP_TIMEZONE') .'") &gt; "' . Carbon::today()-&gt;toDateTimeString() . '"');
  }
  </code></pre>
  
  <p>And this is the query I've built in order to get those posts, with the filters</p>
  
  <pre><code>$posts = App\Models\FacebookPost::with('page')
  -&gt;selectRaw('COUNT(id) AS postCount, SUM(likes) AS likes, SUM(likes)/COUNT(id) AS likesPerPost')
  -&gt;today()
  -&gt;groupBy('facebook_page_id')
  -&gt;orderByRaw('SUM(likes)/COUNT(id) DESC')
  -&gt;get();
  </code></pre>
  
  <p><strong>Problem</strong><br>
  The problem I'm currently having, is, that, when I try to rebuild the above query, I'm not getting all fields I want. As soon as I add an <code>select</code> to the <code>Builder</code>, the <code>relations</code> array, with the index <code>page</code> is null. If I ommit the <code>select</code> method, I'm getting the FacebookPage, but I want to have those specific fields</p>
  
  <p>Now I'm getting an object. I guess this is because I'm using the <code>Eloquent Builder</code> right? Isn't it somehow possible to only get the fields I want to have? The result I'm expecting should look like this (per row)</p>
  
  <pre><code>facebook_name    |    postCount    |    likes    |    likesPerPost
  
  McDonalds             1000              500           0.5
  </code></pre>
  
  <p>I also tried it like this</p>
  
  <pre><code>$posts = App\Models\FacebookPost::with(['page' =&gt; function($query) {
      $query-&gt;select('facebook_name');
      }])
  -&gt;selectRaw('COUNT(id) AS postCount, SUM(likes) AS likes, SUM(likes)/COUNT(id) AS likesPerPost')
  -&gt;today()
  -&gt;groupBy('facebook_page_id')
  -&gt;orderByRaw('SUM(likes)/COUNT(id) DESC')
  -&gt;get();
  </code></pre>
  
  <p>Would I need to use the <code>DB</code> class instead of Eloquent? Or what would be the best solution for this problem?</p>
  
  <p><strong>Alternative solution</strong>  </p>
  
  <pre><code>$pages = DB::table('facebook_posts')
              -&gt;select(DB::raw('facebook_pages.facebook_name, COUNT(facebook_posts.id) AS postCount, SUM(likes) AS likes, ROUND(SUM(likes)/COUNT(facebook_posts.id)) AS likesPerPost'))
              -&gt;join('facebook_pages', 'facebook_posts.facebook_page_id', '=', 'facebook_pages.id')
              -&gt;whereRaw('CONVERT_TZ(`'. $this-&gt;createDateColumn .'`, "UTC", "'. env('APP_TIMEZONE') .'") &gt; "' . Carbon::today()-&gt;toDateTimeString() . '"')
              -&gt;groupBy('facebook_page_id')
              -&gt;orderByRaw('ROUND(SUM(likes)/COUNT(facebook_posts.id)) DESC')
              -&gt;get();
  </code></pre>
  
  <p>This, however, works. Would this be the correct solution for my use case? I'm just asking if it even make sense to use Eloquent here, since I'm not really trying to get an object, but data from multiple sources.</p>