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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

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.


All versions of laravel-macroable-models with dependencies

PHP Build Version
Package Version
No informations.
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package javoscript/laravel-macroable-models contains the following files

Loading the files please wait ....