For Loop在laravel控制器中不起作用

时间:2018-12-12 07:56:22

标签: php laravel loops for-loop eloquent

对于循环不起作用$ AllPathsTasgs有2个数组,所以count($ AllPathsTasgs)将返回2 my并且我将返回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();

1 个答案:

答案 0 :(得分:0)

将此内容写在循环中,我们就那样在外面。

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');


            }]);
        }