title: How can I display other data in foreach?
body: <p>In my show function in which you can watch every single group, I load all articles with the same tags. And the article with the most votes will be displayed separately at the top. In addition, I indicate the average rating of the items in this group. This is how my function looks like:</p>
  
  <pre><code>public function show($id)
      {
          $group = group::find($id);
  
              $tagIdArray = $group-&gt;tags-&gt;pluck('id')-&gt;all();
  
              $mostvotedarticle = Article::where('type', 4)-&gt;whereIn('privacy', [1, 3])
                  -&gt;whereHas('tags', function($query) use ($tagIdArray) {
                      $query-&gt;whereIn('tags.id', $tagIdArray);
                  }, '&gt;=', count($tagIdArray))
                  -&gt;orderByVotes()-&gt;first();
  
              $articledown = Article::where('status', 1)-&gt;whereHas('tags', function($query) use ($tagIdArray) {
                  $query-&gt;whereIn('tags.id', $tagIdArray);
              }, '&gt;=', count($tagIdArray))-&gt;downVotesAll()-&gt;count();
  
              $articleup = Article::where('status', 1)-&gt;whereHas('tags', function($query) use ($tagIdArray) {
                  $query-&gt;whereIn('tags.id', $tagIdArray);
              }, '&gt;=', count($tagIdArray))-&gt;upVotesAll()-&gt;count();
  
              $ratio = null;
  
              if($articleup + $articledown == 0) {
                  $ratio = 0;
              } else {
                  $ratio = ($articleup*100)/($articleup + $articledown);
              }
  
      return view('singlegroup',compact('groups','mostvotedarticle', 'ratio'));
  }
  </code></pre>
  
  <p>On a overview page, the individual groups are displayed with a foreach loop:</p>
  
  <p>function:</p>
  
  <pre><code>public function index()
      {
          $user = Auth::user();
          $groups = $user-&gt;groups()-&gt;latest()-&gt;with('tags')-&gt;paginate(20);
  
          return view('groups', compact('groups'));
      }
  </code></pre>
  
  <p>view:</p>
  
  <pre><code>@foreach ($groups as $group)
          {{$group-&gt;name}}                      
          &lt;img src="-----load mostvotedarticle that is in this group------" alt="" /&gt;
          &lt;div class="progress-bar-primary" style="width:{{$ratio}}%;"&gt;
  @endforeach 
  </code></pre>
  
  <p>How can I have the mostvotedarticle and group ratings displayed in the foreach loop?</p>
  
  <p>Article Model</p>
  
  <pre><code>public function scopeOrderByVotes($query)
      {
          $query-&gt;leftJoin('Article_activities', 'Article_activities.article_id', '=', 'articles.id')
              -&gt;selectRaw('articles.*, count(Article_activities.id) as aggregate')
              -&gt;groupBy('articles.id')
              -&gt;orderBy('aggregate', 'desc');
      }
  
      public function scopeDownVotesAll($query)
      {
          $query-&gt;leftJoin('Article_activities', 'Article_activities.article_id', '=', 'articles.id')
              -&gt;selectRaw('articles.*, count(Article_activities.id) as aggregate')
              -&gt;where('Article_activities.activity', '=', '0');
      }
  
      public function scopeUpVotesAll($query)
      {
          $query-&gt;leftJoin('Article_activities', 'Article_activities.article_id', '=', 'articles.id')
              -&gt;selectRaw('articles.*, count(Article_activities.id) as aggregate')
              -&gt;where('Article_activities.activity', '=', '1');
      }
  </code></pre>