Extend Eloquent relationships to support implied polymorphism and merging
Use composer:
composer require danhunsaker/eloquent-meta-relate
Nothing to it!
Include the Danhunsaker\Eloquent\Traits\MetaRelate
trait on any model you want
to extend:
use Danhunsaker\Eloquent\Traits\MetaRelate;
use Illuminate\Database\Eloquent\Model;
class BaseModel extends Model
{
use MetaRelate;
}
Now you can create implied polymorphic relationships simply by providing an array of class names, instead of a single class, wherever multiple models are part of the same relationship:
public function files()
{
return $this->hasMany([
'App\Models\Audio',
'App\Models\Document',
'App\Models\Image',
'App\Models\Video',
]);
}
You can also merge existing relationships:
public function forkedTo()
{
return $this->hasMany('App\Models\Fork', 'original_id')->with('fork');
}
public function forkedFrom()
{
return $this->hasMany('App\Models\Fork', 'fork_id')->with('original');
}
public function allForks()
{
return $this->createMetaRelation()
->merge($this->forkedTo())
->merge($this->forkedFrom());
}
Contributions (issues, pull requests, etc) are always welcome on GitHub.
If you find a security issue, please email me directly.