Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/patinthehat/laravel-dynamic-relations
- Owner: patinthehat
- License: mit
- Created: 2017-03-02T14:36:57.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-09-20T01:38:25.000Z (about 4 years ago)
- Last Synced: 2024-04-15T13:53:16.268Z (7 months ago)
- Topics: eloquent, eloquent-models, laravel, laravel-5-package, laravel-package, lumen, lumen-package, php
- Language: PHP
- Size: 9.77 KB
- Stars: 1
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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();
```