如何使用()函数循环laravel?

问题描述 投票:0回答:1

我有一个问题,这是我的代码:我编辑我的任务。

//它仍然是错误,因为$ x是一个数组。

$x = [$arr[0], $arr[1],$arr[2],$arr[3]]; 

返回$ this-> with($ x) - > where('parent_id','=',null) - > get();

//没关系。但我想用函数在laravel里面循环$ arr。

return $this->with($arr[0], $arr[1],$arr[2],$arr[3])->where('parent_id', '=', null)->get();
php laravel relation
1个回答
1
投票

创建一个单独的数组,并循环:

$array = [
    [$child => function ($query) {
        $query->whereIn('id', $this->collectID);
    }],
    [$child . '.' . $child => function ($query) {
        $query->whereIn('id', $this->collectID);
    }],
    [$child . '.' . $child . '.' . $child => function ($query) {
        $query->whereIn('id', $this->collectID);
    }],
    [$child . '.' . $child . '.' . $child . '.' . $child => function ($query) {
        $query->whereIn('id', $this->collectID);
    }],
    [$child . '.' . $child . '.' . $child . '.' . $child . '.' . $child => function ($query) {
        $query->whereIn('id', $this->collectID);
    }]
];
// loop through $array, do what you need and than pass it to the with method below
return $this->with($array)->whereNull('parent_id')->get();
© www.soinside.com 2019 - 2024. All rights reserved.