friendsofhyperf / model-morph-addon
The model morph addon for Hyperf.
Fund package maintenance!
huangdijia
hdj.me/sponsors
Requires
- hyperf/database: ~3.1.0
- hyperf/di: ~3.1.0
- dev-main / 3.1.x-dev
- v3.1.48
- v3.1.41
- v3.1.31
- v3.1.28.1
- v3.1.28
- v3.1.17
- v3.1.5
- v3.1.1
- v3.1.0
- v3.1.0-rc.4
- v3.1.0-beta.20
- v3.1.0-beta.15
- v3.1.0-beta.9
- v3.1.0-beta.1
- 3.0.x-dev
- v3.0.112
- v3.0.85
- v3.0.80
- v3.0.70
- v3.0.55
- v3.0.52
- v3.0.51
- v3.0.35
- v3.0.14
- v3.0.2
- v3.0.0
- v3.0.0-rc.46
- v3.0.0-rc.43
- 2.0.x-dev
- v2.0.28
- v2.0.26
- v1.0.0-beta.3
- v1.0.0-beta2
- v1.0.0-beta1
- v0.1.1
- v0.1.0
This package is auto-updated.
Last update: 2024-12-04 02:41:49 UTC
README
The model morph addon for Hyperf.
Installation
composer require friendsofhyperf/model-morph-addon
Before
<?php namespace App\Model; class Image extends Model { public function imageable() { return $this->morphTo(); } } class Book extends Model { public function images() { return $this->morphMany(Image::class, 'imageable'); } } class User extends Model { public function images() { return $this->morphMany(Image::class, 'imageable'); } } // Global Relation::morphMap([ 'user' => App\Model\User::class, 'book' => App\Model\Book::class, ]);
After
<?php namespace App\Model; class Image extends Model { public function imageable() { return $this->morphTo(); } // Privately-owned public static function getActualClassNameForMorph($class) { $morphMap = [ 'user' => User::class, 'book' => Book::class, ]; return Arr::get($morphMap, $class, $class); } } class Book extends Model { public function images() { return $this->morphMany(Image::class, 'imageable'); } public function getMorphClass() { return 'book'; } } class User extends Model { public function images() { return $this->morphMany(Image::class, 'imageable'); } public function getMorphClass() { return 'user'; } }