nWidart Modules relationships

461 Views Asked by At

I have an app and I want to allow modules in it. I find nWidart /Laravel-modules is the best solution for this. I have used it in the past, but in my previous projects I was the sole developer, so when I created a model inside a module, to create the relationships between it and one of my base models, I just went and edit both files:

In App\Models\Disease I would add a new method:

public function symptoms(){
    return $this->hasMany( Modules\Treatments\Entities\Symptom::class );
}

In Modules\Treatments\Entities\Symptom, the opposite:

public function disease(){
    return $this->belongsTo( App\Models\Symptom::class );
}

Now, I would like to create those relationships, but without writing code in the App\Models files (of course I know there would be some modification required to make it work, I just mean without having to edit the files every time a module is created). Is there a way to do it? Is it possible to work that out?

1

There are 1 best solutions below

0
On

Ok, in the module Entities I created a new model Modules\Treatments\Entities\Disease which extends from App\Models\Disease and I inserted the relationship method there.

use App\Models\Disease as BaseDisease;

class Disease extends BaseDisease{
    /**
     * Relationship between a Disease and it's symptoms
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function symptoms(){
        return $this->hasMany( Modules\Treatments\Entities\Symptom::class );
    }
}

The down side of this approach is that the relationship is only present when I load the Disease from Modules\Treatments\Entities\Disease but I couldn't find another way to do it