greabock / tentacles
Da epic tentacles for Eloquent
Installs: 33 935
Dependents: 0
Suggesters: 0
Security: 0
Stars: 37
Watchers: 5
Forks: 15
Open Issues: 0
Requires
- php: >=5.5.0
- illuminate/database: 5.*
- illuminate/support: 5.*
This package is not auto-updated.
Last update: 2024-11-08 09:57:27 UTC
README
Monkey-patching for eloquent models
Composer
"greabock/tentacles": "dev-master"
user-model...
<? namespace App\User\Models; use Illuminate\Database\Eloquent\Model; use Greabock\Tentacles\EloquentTentacle; User extends Model { use EloquentTentacle; }
ServiceProvider
<?php namespace App\Article\Providers; use Illuminate\Support\ServiceProvider; use App\Article\Models\Article; use App\User\Models\User; use Illuminate\Database\Eloquent\Model; class ArticleProvider extends ServiceProvider { public function register() { #.. } public function boot() { User::addExternalMethod('articles', function() { return $this->hasMany(Article::class); }); User::addExternalMethod('getFullnameAttribute', function() { return $this->first_name . ' ' . $this->last_name; }); } }
Now we can do this:
$user = User::with('articles')->first();
$fullname = $user->fullname;