title: Laravel 8 using group by in view/blade by foreach
body: <p>I have this code in my <code>controller</code>:</p>
  <pre><code>public function videoGallery()
  {
      $videoLinksRecord = VideoLinks::all()-&gt;sortBy('video_category_id')-&gt;groupBy('video_catetgory_id');
  
      return view('video.gallery')-&gt;with('videoLinksRecord', $videoLinksRecord);
  }
  </code></pre>
  <p>which return this query:</p>
  <pre><code>{
      &quot;1&quot;: [
          {&quot;id&quot;:1,&quot;video_category_id&quot;:1,&quot;name&quot;:&quot;Laszlo - Supernova&quot;,&quot;link&quot;:&quot;PKfxmFU3lWY&quot;,&quot;description&quot;:&quot;one loud banger!&quot;,&quot;home_video&quot;:null,&quot;created_at&quot;:&quot;2020-12-31T14:12:13.000000Z&quot;,&quot;updated_at&quot;:&quot;2020-12-31T14:12:13.000000Z&quot;,&quot;deleted_at&quot;:null},
          {&quot;id&quot;:2,&quot;video_category_id&quot;:1,&quot;name&quot;:&quot;Subtact - Restart&quot;,&quot;link&quot;:&quot;M5A46A2txC4&quot;,&quot;description&quot;:&quot;one loud banger!&quot;,&quot;home_video&quot;:null,&quot;created_at&quot;:&quot;2020-12-31T14:12:13.000000Z&quot;,&quot;updated_at&quot;:&quot;2020-12-31T14:12:13.000000Z&quot;,&quot;deleted_at&quot;:null},
          {&quot;id&quot;:3,&quot;video_category_id&quot;:1,&quot;name&quot;:&quot;Sushi Killer &amp; Kevin Villeco - Anime Bae&quot;,&quot;link&quot;:&quot;YHHjfYxLyuA&quot;,&quot;description&quot;:&quot;one loud banger!&quot;,&quot;home_video&quot;:null,&quot;created_at&quot;:&quot;2020-12-31T14:12:13.000000Z&quot;,&quot;updated_at&quot;:&quot;2020-12-31T14:12:13.000000Z&quot;,&quot;deleted_at&quot;:null},
          {&quot;id&quot;:4,&quot;video_category_id&quot;:1,&quot;name&quot;:&quot;Hyper Potions &amp; Subtact - Adventures&quot;,&quot;link&quot;:&quot;TKZUhs9Gcdo&quot;,&quot;description&quot;:&quot;one loud banger!&quot;,&quot;home_video&quot;:null,&quot;created_at&quot;:&quot;2020-12-31T14:12:13.000000Z&quot;,&quot;updated_at&quot;:&quot;2020-12-31T14:12:53.000000Z&quot;,&quot;deleted_at&quot;:null}
      ],
      &quot;2&quot;: [
          {&quot;id&quot;:5,&quot;video_category_id&quot;:2,&quot;name&quot;:&quot;Laszlo - Supernova&quot;,&quot;link&quot;:&quot;PKfxmFU3lWY&quot;,&quot;description&quot;:&quot;one loud banger!&quot;,&quot;home_video&quot;:null,&quot;created_at&quot;:&quot;2020-12-31T14:12:13.000000Z&quot;,&quot;updated_at&quot;:&quot;2020-12-31T14:12:13.000000Z&quot;,&quot;deleted_at&quot;:null},
          {&quot;id&quot;:6,&quot;video_category_id&quot;:2,&quot;name&quot;:&quot;Subtact - Restart&quot;,&quot;link&quot;:&quot;M5A46A2txC4&quot;,&quot;description&quot;:&quot;one loud banger!&quot;,&quot;home_video&quot;:null,&quot;created_at&quot;:&quot;2020-12-31T14:12:13.000000Z&quot;,&quot;updated_at&quot;:&quot;2020-12-31T14:12:13.000000Z&quot;,&quot;deleted_at&quot;:null},
          {&quot;id&quot;:7,&quot;video_category_id&quot;:2,&quot;name&quot;:&quot;Sushi Killer &amp; Kevin Villeco - Anime Bae&quot;,&quot;link&quot;:&quot;YHHjfYxLyuA&quot;,&quot;description&quot;:&quot;one loud banger!&quot;,&quot;home_video&quot;:null,&quot;created_at&quot;:&quot;2020-12-31T14:12:13.000000Z&quot;,&quot;updated_at&quot;:&quot;2020-12-31T14:12:13.000000Z&quot;,&quot;deleted_at&quot;:null}
      ]
  }
  </code></pre>
  <p>I want to display the result grouped by its video category similar to this <code>blade</code>:</p>
  <pre class="lang-xml prettyprint-override"><code>{{-- name of the category --}}
  &lt;h2 class=&quot;poppins-medium text-lg text-darkergreen text-center&quot;&gt;Video Category Name Here&lt;/h2&gt;
  &lt;div class=&quot;container-fluid&quot;&gt;
      &lt;div class=&quot;row&quot;&gt;
          &lt;div class=&quot;col-4&quot;&gt;&lt;/div&gt;
          &lt;div class=&quot;col-4 borderline-darkergreen-md&quot;&gt;&lt;/div&gt;
          &lt;div class=&quot;col-4&quot;&gt;&lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  {{-- loop for each result sharing the same &quot;video_category_id&quot; --}}
  &lt;div class=&quot;container-fluid&quot;&gt;
      &lt;div class=&quot;row d-flex justify-content-center px-0&quot;&gt;
          &lt;div class=&quot;col col-lg-2 d-none d-lg-block d-xl-none&quot;&gt;&lt;/div&gt;
          &lt;div class=&quot;col col-xl-2 d-none d-xl-block&quot;&gt;&lt;/div&gt;
          {{-- video --}}
          &lt;div class=&quot;col-12 col-md-6 col-lg-4 container-fluid d-flex justify-content-center spacer-md&quot;&gt;
              &lt;div class=&quot;row d-flex justify-content-center&quot;&gt;
                  &lt;div class=&quot;col-12 d-flex justify-content-center container-fluid px-0&quot;&gt;
                      {{-- video border --}}
                      &lt;div class=&quot;video-box-border position-relative spacer-md&quot;&gt;
  
                          &lt;a href=&quot;&quot;&gt;
                              {{-- video card image --}}
                              &lt;div class=&quot;video-image position-absolute&quot;&gt;&lt;/div&gt;
                              {{-- video card background --}}
                              &lt;div class=&quot;video-card&quot;&gt;&lt;/div&gt;
                          &lt;/a&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;container-fluid&quot;&gt;
                      &lt;div class=&quot;row&quot;&gt;
                          &lt;div class=&quot;col-2 px-0&quot;&gt;&lt;/div&gt;
                          &lt;div class=&quot;col-8 borderline-darkergreen-sm px-0&quot;&gt;&lt;/div&gt;
                          &lt;div class=&quot;col-2 px-0&quot;&gt;&lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  {{-- video name--}}
                  &lt;p class=&quot;poppins-normal text-md text-gray text-center&quot;&gt;Video Name Here
                  &lt;/p&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  <p>How should I use the <code>$videoLinksRecord</code> in a <code>foreach</code> logic to display it by its <code>groupBy</code> logic? This is my first time grouping query result so I do not know how to construct the logic for the said desired result.</p>
  <p><strong>Edit</strong> the code below is the relationship code in the <code>model</code>:</p>
  <p>VideoLinks Model:</p>
  <pre><code>public function categories()
      {
          return $this-&gt;hasMany(VideoCategories::class);
      }
  </code></pre>
  <p>VideoCategories Model:</p>
  <pre><code>public function links()
      {
          return $this-&gt;belongsTo(VideoLinks::class);
      }
  </code></pre>