【问题标题】:For Loop not Working In laravel ControllerFor循环在laravel控制器中不起作用
【发布时间】:2019-05-13 06:52:25
【问题描述】:

For 循环不工作 $AllPathsTasgs 有 2 个数组所以 count($AllPathsTasgs) 将返回 2 个我的并且我返回 2 个路径所以它应该首先使用

$x == 0

那么应该使用

$x == 1

但它使用

$x == 1

对于拖车,路径不知道为什么

虽然我用过 dd($x);它正确返回 $x

请帮忙

foreach ($paths as $path){
    $AllPathsTasgs[]  = $path->pathtags->pluck('name');
}

$posts3 = Path::with(['ProgrammingField','pathtags' => function ($q) use ($AllPathsTasgs) {
    for ($x = 0; $x < count($AllPathsTasgs); $x++) {
        if ($x == 0) {
            $q->with(['Tasks' => function ($q) use ($AllPathsTasgs, $x) {
                $q->has('tasktags', '=', 2)
                    ->orderBy('id', 'ASC')
                    ->whereDoesntHave('tasktags', function ($query) use ($AllPathsTasgs, $x) {
                        $query->whereNotIn('name', $AllPathsTasgs[0]);
                    })->with('tasktags')->orderBy('id', 'ASC');
            }]);
        }

        if ($x == 1) {
            $q->with(['Tasks' => function ($q) use ($AllPathsTasgs, $x) {
                $q->has('tasktags', '=', 2)
                    ->orderBy('id', 'ASC')
                    ->whereDoesntHave('tasktags', function ($query) use ($AllPathsTasgs, $x) {
                        $query->whereNotIn('name', $AllPathsTasgs[1]);
                    })->with('tasktags')->orderBy('id', 'ASC');
            }]);
        }
    }
}])->whereIn('id',$pathId)->get();

【问题讨论】:

    标签: php laravel loops for-loop eloquent


    【解决方案1】:

    把这个写在循环里面,我们就在外面。

    foreach ($paths as $path){
                    $AllPathsTasgs[]  = $path->pathtags->pluck('name');    
            $posts3 = Path::with(['ProgrammingField','pathtags' => function ($q) use ($AllPathsTasgs) {
                        for ($x = 0; $x < count($AllPathsTasgs); $x++) {
                            if ($x == 0 ){
    
                        $q->with(['Tasks' => function ($q) use ($AllPathsTasgs,$x)  {
    
                            $q->has('tasktags', '=', 2)
                        ->orderBy('id', 'ASC')
    
                        ->whereDoesntHave('tasktags', function ($query) use ($AllPathsTasgs,$x) {
                                   $query->whereNotIn('name', $AllPathsTasgs[0]);
    
                        })->with('tasktags')->orderBy('id', 'ASC');   
                    }]);
                }             
                if ($x == 1 ){
    
                    $q->with(['Tasks' => function ($q) use ($AllPathsTasgs,$x)  {
    
                        $q->has('tasktags', '=', 2)
                    ->orderBy('id', 'ASC')
    
                    ->whereDoesntHave('tasktags', function ($query) use ($AllPathsTasgs,$x) {
                               $query->whereNotIn('name', $AllPathsTasgs[1]);
    
    
                    })->with('tasktags')->orderBy('id', 'ASC');
    
    
                }]);
            }
    

    【讨论】:

    • 我得到相同的结果请帮助
    • 我已尝试将 for 循环输出完整路径代码,结果相同
    猜你喜欢
    • 2016-08-05
    • 2021-10-19
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 2017-04-13
    相关资源
    最近更新 更多