lazychaser / laravel-nestedset

Effective tree structures in Laravel 4-8
3.66k stars 472 forks source link

The 'parent' relationship missing in Model toJson() #507

Open saskaak opened 3 years ago

saskaak commented 3 years ago

Copypasted from another issue:

The fix you provided makes impossibile to call parent relationship in toArray() or toJson() methods.... is there any official workaround for this? Or at least it should be clearly stated in the docs...Spent a hour debugging this, it's a major problem imho

Originally posted by @Silentscripter in https://github.com/lazychaser/laravel-nestedset/issues/17#issuecomment-284152396

saskaak commented 3 years ago

A workaround that worked for my use case:

$categories = Category::get()
    ->linkNodes()
    ->map(fn($category) => collect($category)->merge([
        'parent' => $category->parent->uuid ?? null,
    ]));

With this I get a flat collection of all categories and each category has key parent with just the UUID. The trick is to call collect($category), which unsets the parent key, and then add it back using merge().


It would be nice if this was documented somewhere. It took a non-trivial amount of debugging for me to figure this out.

sawirricardo commented 2 years ago

is this fixed? I think this is still is an issue