Download the PHP package jutia-dev/laravel-category without Composer
On this page you can find all versions of the php package jutia-dev/laravel-category. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jutia-dev/laravel-category
More information about jutia-dev/laravel-category
Files in jutia-dev/laravel-category
Package laravel-category
Short Description Associate Laravel models to a category/categories
License MIT
Informations about the package laravel-category
Assets
php artisan vendor:publish --provider="JutiaDev\Category\CategoryServiceProvider"
Relationships
- All models using the
hasCategories
trait will have access to the following relation$model->categories
that will fetch all categories associated to this$model
-
All models using the
hasCategory
trait will have access to the following relation$model->category
that will fetch the category associated to this$model
- You can fetch all models associated to a
$category
through$category->{$relationshipName}
, where$relationshipName
will be deduced by default from the model class- e.g.
$relationshipName
from 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 thehasCategories
orhasCategory
trait
- 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
hasCategory
orhasCategories
trait- Changing the trait of a model that was using for example the
hasCategory
trait tohasCategories
or the inverse- Changing the value of
cascadeDelete
on one of the models that uses thehasCategory
trait- Removing either the
hasCategory
orhasCategories
from a model that was using either one of them
1 - A note on the cascadeDelete
- By default
cascadeDelete
will be false on the model that is using thehasCategory
trait, 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 thehasCategory
trait:protected bool $cascadeDelete = true;
2 - Impact of re-running migrations
- The Categorizable table will be created only if any eloquent model start using the
hasCategories
trait and the table does not exist already -
The Categorizable table will be deleted if no model is using the
hasCategories
trait and the table already exist -
if a model switches from the
hasCategories
to thehasCategory
trait then a new column referencing the category id (foreign key) will be added to the model. however if theCategorizable
table is not deleted (check above point) then data will remain in theCategorizable
table - If a model that was using the
hasCategories
trait 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
hasCategories
trait switchs to thehasCategories
trait then the foreign key column referencing the category_id will be deleted but all data will be migrated to thecategorizable
table 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