hbliang / laravel-attributes-replication
Laravel Attributes Replication
Installs: 15 861
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.0
- illuminate/contracts: ^6.0|^7.0|^8.0
- illuminate/database: ^6.0|^7.0|^8.0
- illuminate/support: ^6.0|^7.0|^8.0
Requires (Dev)
- orchestra/testbench: ^4.0|^5.0|^6.0
- phpunit/phpunit: ^8.0 || ^9.0
This package is auto-updated.
Last update: 2025-03-27 01:06:40 UTC
README
Installation
composer require hbliang/laravel-attributes-replication
Configuration
php artisan vendor:publish --provider="Hbliang\AttributesReplication\ReplicationServiceProvider"
Usage
companies
id - integer
name - string
users
id - integer
company_id - integer
company- string
use Hbliang\AttributesReplication\Contracts\AttributesReplicatable; use Hbliang\AttributesReplication\Traits\HasAttributesReplication; use Illuminate\Database\Eloquent\Model; class Company extends Model implements AttributesReplicatable { use HasAttributesReplication; protected $fillable = ['name']; public static function registerAttributesReplication() { self::addAttributesReplication() ->map([ 'name' => 'company_name', ]) // or use extra // ->extra(function(Company $company) { // return [ // 'company_name' => $company->name, // ]; // }) ->relation('users') ->event('saved'); } public function users() { return $this->hasMany(User::class); } } class User extends Model implements AttributesReplicatable { use HasAttributesReplication; protected $fillable = ['company_name']; public static function registerAttributesReplication() { self::addAttributesReplication() ->passive() ->map([ 'name' => 'company_name', ]) ->relation('company') ->event('created'); } public function company() { return $this->hasMany(Company::class); } }