Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 使用Map迭代时出现Laravel问题-(在Map函数外部更新全局变量)_Loops_Laravel 5_Laravel Eloquent - Fatal编程技术网

Loops 使用Map迭代时出现Laravel问题-(在Map函数外部更新全局变量)

Loops 使用Map迭代时出现Laravel问题-(在Map函数外部更新全局变量),loops,laravel-5,laravel-eloquent,Loops,Laravel 5,Laravel Eloquent,我有一个控制器,它应该返回一个类别列表。每一个都应该有一个子类别的集合。每个子类别还应具有模块。模块有两种类型:最新模块和最受关注的模块 我想要一个摘要结构: [ { id: 1, name: category 1, sub_categories: [ { id: 1, name: subcategory

我有一个控制器,它应该返回一个类别列表。每一个都应该有一个子类别的集合。每个子类别还应具有模块。模块有两种类型:最新模块和最受关注的模块

我想要一个摘要结构:

[
    {
        id: 1,
        name: category 1,
        sub_categories: 
            [
                {
                    id: 1,
                    name: subcategory 1:
                    modules:
                    [
                        latestModules: [...],
                        mostViewedModules[...]

                    ]
                }
            ],
        another sub-category object,
        and another sub-category object
    },
    another category object,
    and another category object,
]
我的控制器

class SeriesController extends Controller
{
    public function getSeries()
    {

        $categories = Category::with('SubCategories')->get();

        $countCats = 0;
        $countSubCats = 0;

        collect($categories)->map(function ($category) use ($countCats, $countSubCats, $categories){

            collect($category->subCategories)->map(function ($subCategory) use ($countSubCats, $categories) {

                collect($categories[$countCats]->subCategories[$countSubCats])
                                    ->put('modules',
                                        SubCategory::with('latestModules', 'mostViewedModules')
                                                    ->where('id', $subCategory->id)->get()
                                    );
                $countSubCats++;
            });

            $countCats++;

        });

        return $categories;

    }
}
我的分类模型

class Category extends Model
{

    public function subCategories()
    {
        return $this->hasMany('App\SubCategory')
                    ->orderby('name');
    }

}
我的子类别模型

    class SubCategory extends Model
{

    public function parentCategory(){
        return $this->belongsTo('App\Category', 'category_id');
    }

    public function modules()
    {
        return $this->belongsToMany('App\Module', 'module_category', 'sub_category_id', 'module_id');
    }

    public function latestModules()
    {
        return $this->modules()
                    ->orderBy('created_at', 'desc');
    }

    public function mostViewedModules()
    {
        return $this->modules()
                    ->orderBy('views', 'desc');
    }

}
我的模块模型

class Module extends Model
{

    public function subCategories()
    {
        return $this->belongsToMany('App\SubCategory', 'module_category', 'module_id', 'sub_category_id');
    }
}
我想做什么

1) Get all the categories via Eloquent Query

2) Collect the many categories and Map over them

3) For each ONE category i map over again to single it out into sub-categories

4) For each sub-category i run Eloquent query to return the related modules.
5) Then i put that result into categories original array by using PUT. I wanted to expand on the original $categories array by including the modules.
问题

我意识到循环可以工作,一切都很好,只是全局$categories变量不更新。它类似于循环内部的更新,但一旦我们存在,贴图循环的值默认为

Category::with('SubCategories')->get()