在视图中运行嵌套循环的优雅方式(Laravel)


Elegant way to run nested loops in views (Laravel)

我正在用Laravel开发一个列出脚本的网站。在我的视图文件中,我需要循环浏览脚本,分别列出满足某些条件的脚本的子脚本。我想在控制器中包含所有功能,但我不知道如何将特定于循环脚本的信息传递到视图。参见以下示例:

//CONTROLLER
 class ScriptController extends BaseController {
        public function instance() 
    {
        $scripts = Script::all();
        return View::make('script')->with(array(    
            'scripts' => $scripts
        )); 
    }


    // VIEW
    @foreach ($scripts as $script)
    TITLE: {{ $script->title }}<br />
    VOTES: 
    @foreach ($script->votes as $v)
        {{ $v['vote'] }}, 
        // This just lists out each vote (which is 1 or -1)
        // how do I count all 1's then all the -1's separately without a where query?
        @endforeach
    @endforeach

最终,我需要在视图中做下面这样的事情,或者以某种方式通过控制器传递信息。

    // IMPOSSIBLE VIEW
    @foreach ($scripts as $script)
        TITLE: {{ $script->title }}<br />
        VOTES: 
        @foreach ($script->votes as $v)
            GOOD VOTES: {{ count($v where $v['vote'] == 1) }}, 
            BAD VOTES: {{ count($v where $v['vote'] == -1) }}
        @endforeach
        @endforeach

我缺少什么想法或基本的控制器概念吗?

更新希望这能稍微解决我的问题。昨晚,我创建了一个简单的控制器,它完美地传递信息,包括显示单个脚本所需的单个脚本的所有子类。并且可以完美地创建该视图。有什么方法可以循环这种观点吗。

请注意,我必须在控制器中物理地包含一个脚本ID($sid)才能使其工作。

    //Controller Function
        public function instance() 
    {
        $sid = 27; //HAD TO PHYSICALLY INCLUDE SCRIPT ID
        $script = Script::find($sid);
        $user = $script->user()->first();
        $uid = $user['id'];
        $votes = array();
        $username = $user['username'];
        $good = Vote::good()->where('script_id', "=", "$sid")->count();
        $bad = Vote::bad()->where('script_id', "=", "$sid")->count();
        $voteQuery = Vote::where('script_id', "=", "$sid")->get();
        foreach($voteQuery as $v) {
                $votes[$v['user_id']] = $v['vote']; 
        }

        return View::make('script')->with(array(
            'script' => $script, //array
            'votes' => $votes, //array
            'user' => $user, //array
            'username' => $username,
            'good' => $good,
            'bad' => $bad
        )); 
    }

然后在我的视图中显示单个脚本:

    //VIEW 
    TITLE: {{$script->title}}<br />
    UP VOTES: {{ count($good)}}<br />
    DOWN VOTES: {{ count($bad)}}<br />
    USERNAME: {{$username}}<br />
    @foreach($votes as $k => $v)
        {{ $k }} - {{ $v }}<br />
    @endforeach

有什么方法可以循环显示所有脚本吗?通过循环遍历$script_id数组并将其传递给每个循环中的控制器?

我建议您在将$scripts数组传递到视图之前,在控制器中对其进行预处理。

//CONTROLLER
class ScriptController extends BaseController {
    public function instance() 
    {
        $scripts = Script::all();
        // do good and bad vote summations
        foreach ($scripts->votes as $idx => $v ) {
            // initialize the counters
            if ( $idx == 0 ) {
                $v['Good'] = 0;
                $v['Bad'] = 0;
            }
            if ( $v['vote'] == 1 ) {
                $v['Good']++;
            }
            if ( $v['vote'] == -1 ) {
                $v['Bad']++;
            }
        }
        return View::make('script')->with(array('scripts' => $scripts)); 
    }
}

然后,您可以在视图中显示["好"]和["坏"]计数器。

Whew。24小时断断续续,这里是:

和往常一样,答案很简单,让我觉得自己很笨。我可以循环浏览控制器中的所有脚本,然后通过"回显"视图而不是"返回"控制器中的视图,它会多次渲染视图。

//CONTROLLER
public function instance() 
{
    $allScripts = Script::orderBy('created_at', 'DESC')->paginate(10);
    foreach($allScripts as $s) {
        $sid = $s['id'];
        $script = Script::find($sid);
        $user = $script->user()->first();
        $uid = $user['id'];
        $votes = array();
        $username = $user['username'];
        $good = Vote::good()->where('script_id', "=", "$sid")->count();
        $bad = Vote::bad()->where('script_id', "=", "$sid")->count();
        $voteQuery = Vote::where('script_id', "=", "$sid")->get();
        foreach($voteQuery as $v) {
                $votes[$v['user_id']] = $v['vote']; 
        }

        echo View::make('script')->with(array(
            'script' => $script, //array
            'votes' => $votes, //array
            'user' => $user, //array
            'username' => $username,
            'good' => $good,
            'bad' => $bad
        )); 
    }
}

然后视图可以是相同的

//VIEW
TITLE: {{$script->title}}<br />
UP VOTES: {{ $good }}<br />
DOWN VOTES: {{ $bad }}<br />
USERNAME: {{$username}}<br />
@foreach($votes as $k => $v)
USER ID: {{ $k }} voted - {{ $v }}<br />
@endforeach
<hr />