Download the PHP package javoscript/laravel-macroable-models without Composer
On this page you can find all versions of the php package javoscript/laravel-macroable-models. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download javoscript/laravel-macroable-models
More information about javoscript/laravel-macroable-models
Files in javoscript/laravel-macroable-models
Package laravel-macroable-models
Short Description A package for adding methods to Laravel models on the fly
License MIT
Informations about the package laravel-macroable-models
Laravel macroable models
A package for adding methods to Laravel models on the fly 🕊
The package offers developers an easy way of programmatically adding methods to Laravel Eloquent models. Behind the scenes, it makes use of Laravel's own macroable trait. For more details, check the post where I explain how I did it in my blog.
Installation
Just install the package with composer
composer require javoscript/laravel-macroable-models
(Only necessary for Laravel <5.5, or if you want to be explicit) - Add the Service Provider to the providers
array in the config/app.php
file
Usage example
The package provides a Facade to facilitate access to it's functionality. Alternatively, you can access it through the app('macroable-models')
helper.
For obvious reasons, macros should be added to the model before other parts of the system make use of it. Because of this, the boot
method of Service Providers is a good place to start adding macros.
For example, adding a method to the \App\User
model in AppServiceProvider
:
After adding the macro to the User
model, now every instance of this Eloquent model will have the sayHi()
method available.
We can quickly verify this within artisan tinker
:
In a dedicated MacrosServiceProvider
file
If you want to keep multiple macro definitions together, then adding a Service Provider for this purpose might be a good idea.
You can generate a new Service Provider with artisan
:
Then, you should add it to the providers
array in the config/app.php
file.
Then, in the boot
method of this new Service Provider, you can centralize macro definitions:
Available methods
The following examples will use the \App\User
model so that you can try the examples on a fresh Laravel application. Any class that extends the Illuminate\Database\Eloquent\Model
class can be extended with these macros.
addMacro(Model::class, 'macroName', function() {}) : void
The most important method of this package, and the one you will most likely be using the most.
Add a macro with the name macroName
to the model Model::class
.
After the macro has been added, you can call the method on the model as you normally would.
With parameters
The defined macro function can receive any number and type of parameters.
Context binding... the correct $this
On the macro function you have access to the $this
object, which references the instance of the model that is executing the function.
Adding relationships
You can define relationship functions too!
Beware! You won't be able to use Laravel's magic relationship attributes.
Overriding existing macro
If you add a macro with the same name of an existing one, it replaces it.
Model's methods precedence
If you add a macro with the same name of an existing method from the model, the latter will take precedence. You won't be able to override it with this package.
removeMacro(Model::class, 'macroName') : boolean
The opposite of addMacro
, this method removes a previously added macro from the specified model. It returns true
if a macro with that name was previously registered on the model and it removed it correctly - and false
otherwise.
Additional goodies
Because, why not? 🤷♂
getAllMacros() : Array
Returns all registered macros, grouped by name.
modelHasMacro(Model::class, 'macroName') : boolean
Simple: if the model has the macro, it returns true
- else, it returns false
.
modelsThatImplement('macroName') : Array
Given a macro name, it returns an array with the classes of the models to which it was added.
macrosForModel(Model::class) : Array
Given the model class, it returns an array with all the macros that were added to it, detailing the defined parameters for each.
Related packages
There are some related packages out there, from which some inspiration was taken.