Skip to content

Commit

Permalink
Adding dynamic relations on block
Browse files Browse the repository at this point in the history
  • Loading branch information
nWidart committed Dec 21, 2015
1 parent eddec14 commit 152d330
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@
'edit' => [],
],
],

/*
|--------------------------------------------------------------------------
| Dynamic relations
|--------------------------------------------------------------------------
| Add relations that will be dynamically added to the Block entity
*/
'relations' => [
// 'extension' => function ($self) {
// return $self->belongsTo(BlockExtension::class, 'id', 'block_id')->first();
// }
],
];
16 changes: 16 additions & 0 deletions Entities/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,20 @@ class Block extends Model
protected $casts = [
'online' => 'bool',
];

public function __call($method, $parameters)
{
#i: Convert array to dot notation
$config = implode('.', ['asgard.block.config.relations', $method]);

#i: Relation method resolver
if (config()->has($config)) {
$function = config()->get($config);

return $function($this);
}

#i: No relation found, return the call to parent (Eloquent) to handle it.
return parent::__call($method, $parameters);
}
}

0 comments on commit 152d330

Please sign in to comment.