foreach中的其他数据

时间:2018-09-25 13:51:12

标签: laravel function foreach load

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

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循环显示:

@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 index()
    {
        $user = Auth::user();
        $groups = $user->groups()->latest()->with('tags')->paginate(20);

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

0 个答案:

没有答案