title: How to use "group by" with a polymorphic relationship in Laravel?
body: <p>I have the following relationship:</p>
  
  <pre><code>return $this-&gt;morphedByMany('App\Models\Movie', 'listable', 'cnt_lists', 'list_id')-&gt;withPivot('id', 'order', 'updated_at')-&gt;where('movies.has_poster', true)-&gt;orderBy('order', 'desc')-&gt;withTimestamps();
  </code></pre>
  
  <p>That relationship produces this query:</p>
  
  <pre><code>select `movies`.*, `cnt_lists`.`list_id` as `pivot_list_id`, `cnt_lists`.`listable_id` as `pivot_listable_id`, `cnt_lists`.`id` as `pivot_id`, `cnt_lists`.`order` as `pivot_order`, `cnt_lists`.`updated_at` as `pivot_updated_at`, `cnt_lists`.`created_at` as `pivot_created_at` from `movies` inner join `cnt_lists` on `movies`.`id` = `cnt_lists`.`listable_id` where `movies`.`has_poster` = '1' and `cnt_lists`.`list_id` in ('3176', '3283', '3285', '3287') and `cnt_lists`.`listable_type` = 'App\Models\Movie' order by `order` desc
  </code></pre>
  
  <p>This relationship works, but it grabs many for each list. I am trying to limit to just one per list. I was originally exploring <code>take()</code> but that limits the whole result set, not a per list limit. I then thought I could group by the <code>cnt_lists.list_id</code> which would grab one per list. This seems to get me what I am looking for. Below is the query that works for me, I am just trying to figure out how to use group by in the relationship because it breaks the relationship when I add it.</p>
  
  <pre><code>select `movies`.*, `cnt_lists`.`list_id` as `pivot_list_id`, `cnt_lists`.`listable_id` as `pivot_listable_id`, `cnt_lists`.`id` as `pivot_id`, `cnt_lists`.`order` as `pivot_order`, `cnt_lists`.`updated_at` as `pivot_updated_at`, `cnt_lists`.`created_at` as `pivot_created_at` from `movies` inner join `cnt_lists` on `movies`.`id` = `cnt_lists`.`listable_id` where `movies`.`has_poster` = '1' and `cnt_lists`.`list_id` in ('3176', '3283', '3285', '3287') and `cnt_lists`.`listable_type` = 'App\Models\Movie' group by `cnt_lists`.`list_id` order by `order` desc
  </code></pre>
  
  <p>When I add <code>groupBy</code> in the relationship:</p>
  
  <pre><code>return $this-&gt;morphedByMany('App\Models\Movie', 'listable', 'cnt_lists', 'list_id')-&gt;withPivot('id', 'order', 'updated_at')-&gt;where('movies.has_poster', true)-&gt;groupBy('cnt_lists.list_id')-&gt;orderBy('order', 'desc')-&gt;withTimestamps();
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>select `movies`.*, `cnt_lists`.`list_id` as `pivot_list_id`, `cnt_lists`.`listable_id` as `pivot_listable_id`, `cnt_lists`.`id` as `pivot_id`, `cnt_lists`.`order` as `pivot_order`, `cnt_lists`.`updated_at` as `pivot_updated_at`, `cnt_lists`.`created_at` as `pivot_created_at` from `movies` inner join `cnt_lists` on `movies`.`id` = `cnt_lists`.`listable_id` where `movies`.`has_poster` = 1 and `cnt_lists`.`list_id` in (3176, 3283, 3285, 3287) and `cnt_lists`.`listable_type` = App\Models\Movie group by `cnt_lists`.`list_id` order by `order` desc
  </code></pre>
  
  <p>For some reason, the model <code>App\Models\Movie</code> is not in quotes so it breaks the whole query. If I run the query manually it works, I just have to add quotes to the polymorphic model. i.e. <code>"App\Models\Movie"</code></p>