asb / morphmtm
This package provides you with a module to automatically files need (Model,Migrations,...) add Morph many-to-many relations to your project along with commonly used basic commands.which minimizes the trial and error operation for you..
Installs: 21
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:package
Requires
- php: >=8.0
This package is auto-updated.
Last update: 2025-06-21 14:09:41 UTC
README
Maybe for you, implementing a series of models and repetitive relationships is boring,
time-consuming and annoying, and you will have to copy your previous codes or refactor them,
which will be a waste of time.
This package provides you with a module to automatically files need (Model,Migrations,...) add Morph many-to-many relations to
your project along with commonly used basic commands.
which minimizes the trial and error operation for you.
how to use this package :
There is no need to define relationships anymore, and it is enough to Add the necessary Trait Has+yourModel (like this => hasStatus) in the models to which the Model is applied.
Requirements
The package requires PHP 8 or higher. The Laravel package also requires Laravel 10 or higher.
Quick Start
-
Installation:
composer require asb/morphmtm
-
publish the package's configuration file by running:
php artisan vendor:publish --tag=morph-mtm-config
-
Autoloading
By default, the module classes are not loaded automatically. You can autoload your modules using psr-4. For example:
"autoload": { "psr-4": { "App\\": "app/", "Rack\\": "Rack/" } }
Tip: don't forget to run composer dump-autoload afterward
In an example to create a category module, I will explain how to use it.
-
build module:
php artisan mtm:build *Modulename*
php artisan mtm:build category
in continue the command, you will be asked to named, of course, you can confirm and pass the default value by pressed the enter key, but some default words may not be to your liking.`
What is the Model Name [Category]: >[enter] What is the Model Plural Name [categories]: >[enter] What is the Model Relation Name [categoryable]: >categorizable //Here the default name of the relationship is not good, and we changed it.
Tip: If You need to remove a module you can use this command
php artisan mtm:remove ModuleName -
Add the necessary Trait to your model:
//The class model requires these trait. use HasCategory; //has+yourModelName
-
Use predefined functions:
-
it gets all the models that have Category.
Tip: all functions is can call static with this format (MTM+modelName):\
one example : MTMCategory::getModelsHave('new')
getModelsHave(string|ID $MTMmodel)
-
it gets all the Categories of Model.
getCategories(Model $model)
-
it checks the Model has this Category by Title or ID.
hasCategries(string|ID $model,string $MTMmodel)
-
it assigns a Category to the Model by Title or ID.
assignCategory(Model $model,string|ID $MTMmodel)
-
it adds a Category to the Model by Title or ID.
addCategory(Model $model,string|ID $MTMmodel)
updateCategory(Model $model,string|ID $MTMmodel,string|ID $newMTMmodel)
removeCategory(Model $model,string|ID $MTMmodel)
removeAllCategory(Model $model)
-
Using CRUD of Model in Module:
createCategoryModel(string $MTMmodel)
-
it gets all of Category And if it is called with "true" parameter, it will get all the deleted Category.
getAllCategoryModel(bool $onlyTrashed=false)
getCategoryModel(string|ID $MTMmodel)
updateCategoryModel(string|ID $MTMmodel, string $update_Category):
removeCategoryModel(string|ID $MTMmodel)
restoreCategoryModel(string|ID $MTMmodel)
-