greabock / tentacles by greabock

Da epic tentacles for Eloquent
33,655
37
5
Package Data
Maintainer Username: greabock
Maintainer Contact: greabock@gmail.com (Greabock)
Package Create Date: 2015-02-16
Package Last Update: 2021-02-03
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-04-23 03:11:59
Package Statistics
Total Downloads: 33,655
Monthly Downloads: 117
Daily Downloads: 6
Total Stars: 37
Total Watchers: 5
Total Forks: 15
Total Open Issues: 0

tentacles

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;