PHPackages                             gawsoft/laravel-macroable-models - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. gawsoft/laravel-macroable-models

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

gawsoft/laravel-macroable-models
================================

Fork from a package for adding methods to Laravel models on the fly

v1.0.0(3y ago)01.4kMITPHP

Since Mar 25Pushed 3y agoCompare

[ Source](https://github.com/gawsoftpl/laravel-macroable-models)[ Packagist](https://packagist.org/packages/gawsoft/laravel-macroable-models)[ RSS](/packages/gawsoft-laravel-macroable-models/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

Fork
====

[](#fork)

This is a fork from original repo javoscript/laravel-macroable-models. Original repo is not updated, and owner do not accept pull requests.

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](https://laravel.com/docs/6.x/eloquent#defining-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](https://javoscript.com/blog/post/adding-macros-to-laravel-eloquent-models).

Installation
------------

[](#installation)

Just install the package with `composer`

> composer require javoscript/laravel-macroable-models

**(Only necessary for Laravel &lt;5.5, or if you want to be explicit)** - Add the Service Provider to the `providers` array in the `config/app.php` file

```
// config/app.php

$providers = [
    // ...
    \Javoscript\MacroableModels\MacroableModelsServiceProvider::class,
    // ...
];
```

Usage example
-------------

[](#usage-example)

The package provides a [Facade](https://laravel.com/docs/6.x/facades) 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](https://laravel.com/docs/6.x/providers) is a good place to start adding macros.

For example, adding a method to the `\App\User` model in `AppServiceProvider`:

```
// app/Providers/AppServiceProvider.php

// ...

use \Javoscript\MacroableModels\Facades\MacroableModels;
use \App\User;

// ...

public function boot()
{

    MacroableModels::addMacro(User::class, 'sayHi', function() {
        return 'Hi!';
    });

}
```

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`:

```
php artisan tinker

>>> \App\User::first()->sayHi()
=> "Hi!"

```

### In a dedicated `MacrosServiceProvider` file

[](#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`:

```
php artisan make:provider MacrosServiceProvider

```

Then, you should add it to the `providers` array in the `config/app.php` file.

```
// config/app.php

$providers = [
    // ...
    App\Providers\MacrosServiceProvider::class,
    // ...
];
```

Then, in the `boot` method of this new Service Provider, you can centralize macro definitions:

```
// app/Providers/MacrosServiceProvider.php

// ...

use \Javoscript\MacroableModels\Facades\MacroableModels;
use \App\User;

// ...

public function boot()
{

    MacroableModels::addMacro(User::class, 'sayHi', function() {
        return 'Hi!';
    });

    MacroableModels::addMacro(User::class, 'sayBye', function() {
        return 'Bye bye';
    });

}
```

Available methods
-----------------

[](#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`

[](#addmacromodelclass-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.

```
MacroableModels::addMacro(\App\User::class, 'sayHi', function() { return "Hi!"; });

\App\User::first()->sayHi();
```

#### With parameters

[](#with-parameters)

The defined macro function can receive any number and type of parameters.

```
MacroableModels::addMacro(\App\User::class, 'say', function(string $something) { return $something; });

$user = \App\User::first();
$user->say("Hello world!");
```

#### Context binding... the correct `$this`

[](#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.

```
MacroableModels::addMacro(\App\User::class, 'getId', function() { return $this->id; });

\App\User::first()->getId();
// 1
```

#### Adding relationships

[](#adding-relationships)

You can define relationship functions too!

```
MacroableModels::addMacro(\App\User::class, 'posts', function() {
    return $this->hasMany(App\Post::class);
});
```

Beware! You won't be able to use Laravel's magic relationship attributes.

```
$user = App\User::first();

$user->posts;
// null
// This will always return null, as the posts attribute wasn't defined

$user->posts()->get()
// This will correctly return the posts Eloquent collection
```

#### Overriding existing macro

[](#overriding-existing-macro)

If you add a macro with the same name of an existing one, it replaces it.

```
MacroableModels::addMacro(\App\User::class, 'greet', function() { return "Hi!"; });
\App\User::first()->greet();
// "Hi!"

MacroableModels::addMacro(\App\User::class, 'greet', function() { return "Hello human"; });
\App\User::first()->greet();
// "Hello human"
```

#### Model's methods precedence

[](#models-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.

```
class Dog extends Illuminate\Database\Eloquent\Model
{
    public function bark()
    {
        return "Woof!";
    }
}

MacroableModels::addMacro(Dog::class, 'bark', function() { return "Miauuu!"; });

$dog = new Dog;
$dog->bark();
// "Woof!"
```

### `removeMacro(Model::class, 'macroName') : boolean`

[](#removemacromodelclass-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.

```
MacroableModels::removeMacro(\App\User::class, 'salute');
// false

MacroableModels::addMacro(\App\User::class, 'salute', function() { return "Hello!"; });

MacroableModels::removeMacro(\App\User::class, 'salute');
// true
```

Additional goodies
------------------

[](#additional-goodies)

Because, why not? 🤷‍♂

### `getAllMacros() : Array`

[](#getallmacros--array)

Returns all registered macros, grouped by name.

```
MacroableModels::addMacro(\App\User::class, 'hi', function() { return "Hi!"; })

MacroableModels::addMacro(\App\Dog::class, 'hi', function() { return "Woof!"; })

MacroableModels::addMacro(\App\User::class, 'bye', function() { return "Bye bye"; })

MacroableModels::getAllMacros()

/*
   [
     "hi" => [
       "App\User" => Closure() {#3362 …2},
       "App\Dog" => Closure() {#3376 …2},
     ],
     "bye" => [
       "App\User" => Closure() {#3366 …2},
     ],
   ]
*/
```

### `modelHasMacro(Model::class, 'macroName') : boolean`

[](#modelhasmacromodelclass-macroname--boolean)

Simple: if the model has the macro, it returns `true` - else, it returns `false`.

```
MacroableModels::modelHasMacro(\App\User::class, 'salute');
// false

MacroableModels::addMacro(\App\User::class, 'salute', function() { return "Hi!"; });
MacroableModels::modelHasMacro(\App\User::class, 'salute');
// true
```

### `modelsThatImplement('macroName') : Array`

[](#modelsthatimplementmacroname--array)

Given a macro name, it returns an array with the classes of the models to which it was added.

```
MacroableModels::addMacro(\App\User::class, 'hi', function() { return "Hi!"; });
MacroableModels::addMacro(\App\Dog::class, 'hi', function() { return "Woof!"; });

MacroableModels::modelsThatImplement('hi');
/*
   [
      "App\User",
      "App\Dog",
   ]
*/
```

### `macrosForModel(Model::class) : Array`

[](#macrosformodelmodelclass--array)

Given the model class, it returns an array with all the macros that were added to it, detailing the defined parameters for each.

```
MacroableModels::addMacro(\App\User::class, 'say', function(String $something) { return $something; });
MacroableModels::addMacro(\App\User::class, 'sum', function(Integer $a, Integer $b) { return $a + $b; });

MacroableModels::macrosForModel(\App\User::class);
/*
   [
     "say" => [
       "name" => "say",
       "parameters" => [
         ReflectionParameter {#3385
           +name: "something",
           position: 0,
           typeHint: "string",
         },
       ],
     ],
     "sum" => [
       "name" => "sum",
       "parameters" => [
         ReflectionParameter {#3357
           +name: "a",
           position: 0,
           typeHint: "Integer",
         },
         ReflectionParameter {#3360
           +name: "b",
           position: 1,
           typeHint: "Integer",
         },
       ],
     ],
   ]
*/
```

Related packages
----------------

[](#related-packages)

There are some related packages out there, from which some inspiration was taken.

- imanghafoori1/eloquent-relativity: [github](https://github.com/imanghafoori1/eloquent-relativity)
- spatie/laravel-collection-macros: [github](https://github.com/spatie/laravel-collection-macros)
- spatie/macroable: [github](https://github.com/spatie/macroable)

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 76.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1143d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ed3bef7ce2453e13dea2d07fdfd0a0cd47564f30adb68e45691244f2e861d0e9?d=identicon)[gawsoft](/maintainers/gawsoft)

---

Top Contributors

[![javoscript](https://avatars.githubusercontent.com/u/11479916?v=4)](https://github.com/javoscript "javoscript (20 commits)")[![gawsoftpl](https://avatars.githubusercontent.com/u/75686400?v=4)](https://github.com/gawsoftpl "gawsoftpl (6 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/gawsoft-laravel-macroable-models/health.svg)

```
[![Health](https://phpackages.com/badges/gawsoft-laravel-macroable-models/health.svg)](https://phpackages.com/packages/gawsoft-laravel-macroable-models)
```

###  Alternatives

[datatables.net/datatables.net-datetime

DateTime picker for DataTables.net

112.3k](/packages/datatablesnet-datatablesnet-datetime)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
