Skip to content
This repository was archived by the owner on Feb 2, 2022. It is now read-only.

Make some models macroable #107

Closed
larsjanssen6 opened this issue Nov 14, 2019 · 7 comments
Closed

Make some models macroable #107

larsjanssen6 opened this issue Nov 14, 2019 · 7 comments

Comments

@larsjanssen6
Copy link
Contributor

Hi,

Would it be an idea to make some models macroable.

For example I need to add an address to the order model. If the order
model was macroable I could add the relation like this:

Boot method in serviceprovider:

Illuminate\Database\Eloquent\Builder::macro(‘address’, function() {
    $model = $this->getModel();
    
    if($model instanceof App\Address) {
         return $model->belongsToMany(App\Address::class);
    }
    unset(static::$macros['address']);
    return $model->address();
 });

Would you accept a PR for this or is there a better way?

Thanks!

@sandervanhooft
Copy link
Contributor

Hi @larsjanssen6 ,

I'm ok with making things macroable. Another way would be to make classes swappable by setting the class in the config, but that would take a lot of work.

@larsjanssen6
Copy link
Contributor Author

Looks like this is not possible on models yet in Laravel:

laravel/ideas#1870

@larsjanssen6
Copy link
Contributor Author

For now I have overwritten the subscription class but maybe in the future we could add this macroable trait if it's supported would be nice :)

@sandervanhooft
Copy link
Contributor

Yeah that was what I was afraid of with the macroable trait...

Just curious, how did you overwrite it?

@larsjanssen6
Copy link
Contributor Author

@sandervanhooft I made a custom class and extend the Subscription mollie class (only need this for Laravel nova).

@sandervanhooft
Copy link
Contributor

Good to know. Thanks!

@javoscript
Copy link

I recently made a little package which allows to add macros to Eloquent models:

https://github.com/javoscript/laravel-macroable-models

Let me know if you try it out and if it is of any use! =)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants