Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/patinthehat/laravel-dynamic-relations

Dynamic Relationship Accessors for Laravel 5 and Lumen 5
https://github.com/patinthehat/laravel-dynamic-relations

eloquent eloquent-models laravel laravel-5-package laravel-package lumen lumen-package php

Last synced: about 1 month ago
JSON representation

Dynamic Relationship Accessors for Laravel 5 and Lumen 5

Awesome Lists containing this project

README

        

### Laravel DynamicRelations
---

An extension of `Illuminate\Database\Eloquent\Model` that allows relationships to be called as dynamic properties, such as:
```php
$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.

```php
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:
```php
$user = User::find(1);
$prop = "abc";
$user->$prop->toArray();
```