Skip to content

bulwarkzero/tentacles

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 

Repository files navigation

tentacles

Monkey-patching for eloquent models

Composer install

composer require bulwark/tentacles:dev-master

user-model...

<? namespace App\User\Models;

use Illuminate\Database\Eloquent\Model;
use Bulwark\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;

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;

About

laravel package

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%