rinvex / laravel-categories

Rinvex Categorizable is a polymorphic Laravel package, for category management. You can categorize any eloquent model with ease, and utilize the power of Nested Sets, and the awesomeness of Sluggable, and Translatable models out of the box.
MIT License
457 stars 68 forks source link

Get all available categories & mark those of them that are attached to specific model #99

Closed SvetoslavStefanov closed 3 years ago

SvetoslavStefanov commented 3 years ago

Hey, I'd like to be able to list all categories (like in a tree view of (UL) lists, one inside the other) and have those categories that are being attached to the model to be selected. My current solution is to fetch once all categories and then model's categories and check which of them are matching. But it would be great if I could do this with one single query (and left join model's categories).

Omranic commented 3 years ago

Actually you can do that currently, check the docs here https://github.com/rinvex/laravel-categories#building-a-tree

For more details, also check the underlying package that we're using as a dependency for nested-sets functionality https://github.com/lazychaser/laravel-nestedset

SvetoslavStefanov commented 3 years ago

You didn't get what I was asking ... This indeed lists categories as the way I wanted, but it doesn't do anything to let me know which one of them is being attached to the model I'm searching for. I want this: Let's say you are publishing a new article. You have to list all available categories that could be attached to this article. So far, so good, the tree view thing works great. But ... I want to edit that article. So I have to list again all available categories and mark as selected those that are already being attached to the currently editing article.

This functionality is missing.

Omranic commented 3 years ago

No actually it's not missing, if I understand you correctly, you need to know which category is attached to your article, which is existing feature. This info is stored in a different table categorizable. You can get that using your article model as follows:

// Get instance of your model
$post = new \App\Models\Post::find(1);

// Get attached categories collection
$post->categories;

Reference from the docs: https://github.com/rinvex/laravel-categories#manage-your-categorizable-model

If that's not what you want, please describe your use case in more details. I'll be happy to help..