patinthehat / laravel-dynamic-relations
dynamic relations for Laravel and Lumen
v1.1.0
2017-03-02 14:40 UTC
Requires
- php: >=5.6.4
This package is not auto-updated.
Last update: 2024-11-09 21:32:31 UTC
README
Laravel DynamicRelations
An extension of Illuminate\Database\Eloquent\Model
that allows relationships to be called as dynamic properties, such as:
$prop = "myProp"; $user->$prop->toArray();
####Installation
Install with composer: composer require patinthehat/laravel-dynamic-relations
####Usage
To use, extend the DynamicModel
class. In the child class, override the $dynamicRelations
array property, adding items that correlate to relation names.
namespace App\Models; use Permafrost\DynamicRelations\DynamicModel; class User extends DynamicModel { public static $dynamicRelations = [ 'abc', 'def', ]; public function abc() { return $this->hasMany('App\Models\Abc'); } public function def() { return $this->hasMany('App\Models\Def'); } }
Now, your relations can be accessed dynamically:
$user = User::find(1); $prop = "abc"; $user->$prop->toArray();