使用Map迭代时的Laravel问题 - (在地图函数外更新全局变量)

时间:2017-10-23 15:07:32

标签: loops laravel-5 laravel-eloquent

我有一个应该返回类别列表的控制器。每个人也应该有一个子类别的集合。每个子类别也应该有模块。这些模块有两种类型:最新模块和最受监视的模块。

我想要的摘要结构:

[
    {
        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变量不会更新。它就像在循环中更新一样,但是一旦我们存在,Map就会将值默认为

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

0 个答案:

没有答案