在视图中 运行 嵌套循环的优雅方式 (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)); 
    }
}

然后您可以只在您的视图中显示 ['Good'] 和 ['Bad'] 计数器。

哇哦。 24 小时开启和关闭,这里是:

通常答案很简单,让我觉得很愚蠢。我可以遍历控制器中的所有脚本,然后通过 "echoing" 视图而不是 "returning" 控制器中的视图,它会多次呈现视图。

//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 />