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
453 stars 68 forks source link

[Proposal] Allow for multiple category models #24

Closed zcwilt closed 4 years ago

zcwilt commented 6 years ago

Currently code only allows for one category model In an admin type UI, I may want to display categories that relate to News, products, blog posts etc.

To achieve this each model needs to have an initial query that loads categories that relate to that model.

While the nestedSet code allows me to have multiple roots, given there is currently only one category model, there is no simple way to display these for each type

The proposal suggests extending the configuration settings from

'models' => [
    'category' => \App\Models\Category::class,
],

to something like

'models' => [
    'category' => \App\Models\Category::class,
    'product-category' => \App\Models\ProductCategory::class,
    'news-category' => \App\Models\NewsCategory::class,
],

To achieve this there needs to be only fairly minor changes to current code.

In the serviceProvider we need to iterate over the config settings

e.g.

    foreach (config('rinvex.categories.models', []) as $modelKey => $modelName) {
        $this->app->singleton('rinvex.categories.'.$modelKey, $modelName);
    }

and in the Categorizable Trait add this method

/**
 * @return string
 */
public function getRinvexModelKey(): string
{
    return config('rinvex.categories.default-model', 'category');
}

This method can be overriden in other categorizable models

e.g.

In a products model

public function getRinvexModelKey()
{
    return 'product-category';
}

In the Categorizable trait references to config('rinvex.categories.models.category') change to config('rinvex.categories.models.' . $this->getRinvexModelKey()) and references to app('rinvex.categories.category') change to app('rinvex.categories.' . $this->getRinvexModelKey())

mostafaznv commented 6 years ago

you can handle it with type column in categories table

Wikiki commented 5 years ago

Hi @mostafaznv,

I don't see any type column in categories table when looking at the migration file. Where did you get it ?

mostafaznv commented 5 years ago

Hi @mostafaznv,

I don't see any type column in categories table when looking at the migration file. Where did you get it ?

I added it to migration before migrate.

joooseb commented 4 years ago

You can do this with a Inner Join ;). I will paste the code tomorrow!

yo5ofj commented 4 years ago

You can do this with a Inner Join ;). I will paste the code tomorrow!

Can you please paste the code? Thanks.

mbryne commented 4 years ago

@joooseb any update on that code?

Omranic commented 4 years ago

Honestly, I don't see any need for this in my opinion, as you can achieve the same results using the package as it is with the current structure, as one model, and that's using the polymorphic relations used already by default. Example on how to use it already documented here https://github.com/rinvex/laravel-categories#retrieve-all-models-attached-to-the-category

All what you need is the following code:

$category = app('rinvex.categories.category')->find(1);
$category->entries(\App\Models\Post::class);

That way you can retrieve categories related to products, news, or any unlimited number of models actually. Happy coding, and please feel free to re-open for further discussion 🙂

midorikocak commented 3 years ago

what happens when we need product categories and contact categories? should we have to keep them in the same table?

Omranic commented 3 years ago

By default yes, you can create a root category for each, then put sub-categories inside as children.

MostafaNorzade commented 2 years ago

what happens when we need product categories and contact categories? should we have to keep them in the same table?

For example, you have a category: Book Then, Both Products and Contact can use this.

That way you can retrieve categories related to products or contact or ...

$category = app('rinvex.categories.category')->find(1);

$products = $category->entries(\App\Models\Product::class);

$contacts = $category->entries(\App\Models\Contact::class);