0

I have these functions in my Category Class:

...
    /**
     * Get the children of current category.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function children()
    {
        return $this->hasMany(Category::class, 'parent_id');
    }

    /**
     * Get the nested children of current category.
     *
     * @return @return \Illuminate\Database\Eloquent\Builder
     */
    public function childrenRecursive()
    {
        return $this->children()->with('childrenRecursive');
    }

    /**
     * Get the category that depends with the given group name.
     *
     * @param string $group
     * @return \Illuminate\Database\Eloquent\Collection|null
     */
    public static function getTheCategory($group)
    {
        $root = Category::where('group', $group)->whereNull('parent_id')->first();
        return $root ? $root->childrenRecursive()->get() : null;
    }

   ...

When I call the getTheCategory function I get this result:

[
    {
        "id": 2,
        "title": "Agricultural",
        "concise": "The category for agricultural products.",
        "created_at": "2019-12-17 18:01:05",
        "updated_at": "2019-12-17 18:01:05",
        "children_recursive": []
    },
    {
        "id": 3,
        "title": "Animal",
        "concise": "The category for animal products.",
        "created_at": "2019-12-17 18:01:05",
        "updated_at": "2019-12-17 18:01:05",
        "children_recursive": []
    },
    {
        "id": 4,
        "title": "Handicrafts",
        "concise": "The category for handicrafts products.",
        "created_at": "2019-12-17 18:01:05",
        "updated_at": "2019-12-17 18:01:05",
        "children_recursive": []
    }
]

Now, how can I change the children_recursive to the children? I want the result like this:

[
  {
   "id": 2,
   "title": "Agricultural",
   "concise": "The category for agricultural products.",
   "created_at": "2019-12-17 18:01:05",
   "updated_at": "2019-12-17 18:01:05",
    "children": []
    },
  ...
]
6
  • using a mutator? it won't change the name but then you can access with children
    – Alberto
    Commented Dec 18, 2019 at 12:45
  • Hi @AlbertoSinigaglia, yes I used a accessor but the result is same. Commented Dec 18, 2019 at 13:00
  • did you consider using as method? something like $this->children()->with('childrenRecursive')->as('children');
    – shahabphp
    Commented Dec 18, 2019 at 13:30
  • @shahabphp the as method is for belongsToMany and it renames the pivot object
    – lagbox
    Commented Dec 18, 2019 at 13:36
  • 1
    Hi @shahabphp, no, it didn't work for me. I overridden the toArray function. Commented Dec 19, 2019 at 4:13

1 Answer 1

-1

You should use laravel's accessor and alias for doing this. See the decoumentation here https://laravel.com/docs/5.7/eloquent-mutators#defining-an-accessor.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.