Skip to content

i-rocky/eloquent-dynamic-relation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

(ABANDONED) Dynamic Eloquent Relationship

See https://laravel.com/docs/8.x/eloquent-relationships#dynamic-relationships

Adds dynamic relationship to Eloquent ORM models

Installation

composer require i-rocky/eloquent-dynamic-relation

Usage

Add the trait Rocky\Eloquent\HasDynamicRelation to your model as following

use Rocky\Eloquent\HasDynamicRelation;

class MyModel extends Model {
  use HasDynamicRelation;
}

Now define a relationship somewhere

In Laravel you can add this in your AppServiceProvider's boot method

MyModel::addDynamicRelation('some_relation', function (MyModel $myModel) {
    return $myModel->hasMany(SomeRelatedModel::class);
});

Now you can use the relation some_relation as if it's defined in your MyModel class.

Problem & Inspiration: https://stackoverflow.com/a/54065463/4452100

About

Adds dynamic relationship to eloquent ORM models

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages