jutia-dev / laravel-category
Associate Laravel models to a category/categories
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/jutia-dev/laravel-category
Requires
- php: >=7.3
- illuminate/database: ^8.0
- illuminate/filesystem: ^8.0
This package is auto-updated.
Last update: 2025-12-17 08:59:03 UTC
README
php artisan vendor:publish --provider="JutiaDev\Category\CategoryServiceProvider"
Relationships
-
All models using the
hasCategoriestrait will have access to the following relation$model->categoriesthat will fetch all categories associated to this$model -
All models using the
hasCategorytrait will have access to the following relation$model->categorythat will fetch the category associated to this$model -
You can fetch all models associated to a
$categorythrough$category->{$relationshipName}, where$relationshipNamewill be deduced by default from the model class- e.g.
$relationshipNamefrom Product model will be products therefore you can access it with$category->products, you can get the relationName of a model with the helper functiondeduce_relationship_name_from_model(\App\Models\Product::class) - You can customize the relationshipName by adding this variable (
protected string $relationshipName = 'customProducts';) on the model that is using thehasCategoriesorhasCategorytrait
- e.g.
Migration
When to re-run migrations of the category package using the
php artisan migrate_category:refresh:
- Adding a new model that uses either
hasCategoryorhasCategoriestrait - Changing the trait of a model that was using for example the
hasCategorytrait tohasCategoriesor the inverse - Changing the value of
cascadeDeleteon one of the models that uses thehasCategorytrait - Removing either the
hasCategoryorhasCategoriesfrom a model that was using either one of them
1 - A note on the cascadeDelete
- By default
cascadeDeletewill be false on the model that is using thehasCategorytrait, therefore the foreign key will be set to null when the associated category is deleted. if you want the model to be deleted too then you'll need this variable on the model using thehasCategorytrait:protected bool $cascadeDelete = true;
2 - Impact of re-running migrations
-
The Categorizable table will be created only if any eloquent model start using the
hasCategoriestrait and the table does not exist already -
The Categorizable table will be deleted if no model is using the
hasCategoriestrait and the table already exist -
if a model switches from the
hasCategoriesto thehasCategorytrait then a new column referencing the category id (foreign key) will be added to the model. however if theCategorizabletable is not deleted (check above point) then data will remain in theCategorizabletable -
If a model that was using the
hasCategoriestrait no longer uses any of the category trait then the foreign key column referencing the category_id will be deleted -
If a model that was using the
hasCategoriestrait switchs to thehasCategoriestrait then the foreign key column referencing the category_id will be deleted but all data will be migrated to thecategorizabletable that will be created if it does not exist already.
3 - what does php artisan migrate_category:refresh
- php artisan migrate:refresh --path=/database/migrations/2021_11_13_000001_create_categorizable_table.php
- php artisan migrate:refresh --path=/database/migrations/2021_11_13_000002_add_categories_foreign_key_to_related_models.php