如何在 foreach 中显示其他数据?

How can I display other data in foreach?

在我的显示功能中,您可以在其中观看每个组,我加载了所有具有相同标签的文章。得票最多的文章将单独显示在顶部。此外,我指出了该组中项目的平均评分。这是我的函数的样子:

public function show($id)
    {
        $group = group::find($id);

            $tagIdArray = $group->tags->pluck('id')->all();

            $mostvotedarticle = Article::where('type', 4)->whereIn('privacy', [1, 3])
                ->whereHas('tags', function($query) use ($tagIdArray) {
                    $query->whereIn('tags.id', $tagIdArray);
                }, '>=', count($tagIdArray))
                ->orderByVotes()->first();

            $articledown = Article::where('status', 1)->whereHas('tags', function($query) use ($tagIdArray) {
                $query->whereIn('tags.id', $tagIdArray);
            }, '>=', count($tagIdArray))->downVotesAll()->count();

            $articleup = Article::where('status', 1)->whereHas('tags', function($query) use ($tagIdArray) {
                $query->whereIn('tags.id', $tagIdArray);
            }, '>=', count($tagIdArray))->upVotesAll()->count();

            $ratio = null;

            if($articleup + $articledown == 0) {
                $ratio = 0;
            } else {
                $ratio = ($articleup*100)/($articleup + $articledown);
            }

    return view('singlegroup',compact('groups','mostvotedarticle', 'ratio'));
}

在概览页面上,各个组使用 foreach 循环显示:

函数:

public function index()
    {
        $user = Auth::user();
        $groups = $user->groups()->latest()->with('tags')->paginate(20);

        return view('groups', compact('groups'));
    }

查看:

@foreach ($groups as $group)
        {{$group->name}}                      
        <img src="-----load mostvotedarticle that is in this group------" alt="" />
        <div class="progress-bar-primary" style="width:{{$ratio}}%;">
@endforeach 

如何在 foreach 循环中显示投票最多的文章和组评分?

文章模型

public function scopeOrderByVotes($query)
    {
        $query->leftJoin('Article_activities', 'Article_activities.article_id', '=', 'articles.id')
            ->selectRaw('articles.*, count(Article_activities.id) as aggregate')
            ->groupBy('articles.id')
            ->orderBy('aggregate', 'desc');
    }

    public function scopeDownVotesAll($query)
    {
        $query->leftJoin('Article_activities', 'Article_activities.article_id', '=', 'articles.id')
            ->selectRaw('articles.*, count(Article_activities.id) as aggregate')
            ->where('Article_activities.activity', '=', '0');
    }

    public function scopeUpVotesAll($query)
    {
        $query->leftJoin('Article_activities', 'Article_activities.article_id', '=', 'articles.id')
            ->selectRaw('articles.*, count(Article_activities.id) as aggregate')
            ->where('Article_activities.activity', '=', '1');
    }

您需要将所有内容放入您的 foreach

public function index() 
{ 
 $user = Auth::user(); 
 $groups = $user->groups()->latest()->with('tags')->paginate(20); 
 $ratio = null; 
 foreach($groups as $key => $group) 
 { 
  $tagIdArray = $group->tags->pluck('id')->all(); 
  $mostvotedarticle = Article::where('type', 4)->whereIn('privacy', [1, 3]) 
  ->whereHas('tags', function($query) use ($tagIdArray) { 
  $query->whereIn('tags.id', $tagIdArray); 
  }, '>=', count($tagIdArray)) 
  ->orderByVotes()->first(); 

 $articledown = Article::where('status', 1)->whereHas('tags', function($query) use 
 ($tagIdArray) { 
 $query->whereIn('tags.id', $tagIdArray); 
 }, '>=', count($tagIdArray))->downVotesAll()->count(); 

$articleup = Article::where('status', 1)->whereHas('tags', function($query) use 
($tagIdArray) { 
$query->whereIn('tags.id', $tagIdArray); 
}, '>=', count($tagIdArray))->upVotesAll()->count(); 


if($articleup + $articledown == 0) { 
$ratio = 0; 
} else { 
$ratio = ($articleup*100)/($articleup + $articledown); 
} 

$group->mostVote = $mostvotedarticle; 
$group->ratio = $ratio; 
 } 
  return view('groups', compact('groups')); 
}