Hi everyone,
Recently I started a modular project.
The main Idea is starting with a simple core module and each upper module extends project functionality.
It is actually so great but there is a single important problem and that is extending Eloquent models using Laravel's macroable trait.
I found a post by Esmail Zareh on Medium discussing Dynamic Eloquent Relation By Macro and It is actually working well but yet no auto completion is supported and more importantly when extending a model have to make sure if it is the correct model to extend like this :
Illuminate\Database\Eloquent\Builder::macro(‘categories’, function() {
$model = $this->getModel();
if($model instanceof App\Post) {
return $model->belongsToMany(App\Category::class);
}
unset(static::$macros['categories']);
return $model->categories();
});
Here is my question: Is there any right way to achieve Eloquent model extending not having described problems ?