dex / anything
可以是任何东西
dev-main
2024-07-29 16:32 UTC
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0
- illuminate/support: ^10.0||^11.0
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
This package is auto-updated.
Last update: 2024-08-29 16:52:46 UTC
README
您总是需要在数据库中创建辅助表来存储少量记录,以实现良好的数据库规范化。使用 anything
包,这将完成。
安装
您可以通过 composer 安装此包。
composer require dex/anything
现在,您可以使用 Anything
模型创建任何虚拟/逻辑模型,或者扩展它。
属于关系
假设您有一个 Person
模型,该模型与 Gender
、Race
和 Religion
模型存在 belongsTo
关系,对于您的模型,您应该使用 4 个数据库表,如下所示。
class Person extends Model { protected $table = 'person'; public function gender(): BelongsTo { return $this->belongsTo(Gender::class); } public function race(): BelongsTo { return $this->belongsTo(Race::class); } public function religion(): BelongsTo { return $this->belongsTo(Religion::class); } } class Gender extends Model { protected $table = 'gender'; } class Race extends Model { protected $table = 'race'; } class Religion extends Model { protected $table = 'religion'; }
对于每个模型,您都将有一个数据库表。
但使用 Anything
模型,您将只有 2 个数据库表。
class Person extends Model { protected $table = 'person'; public function gender(): BelongsTo { return $this->belongsTo(Gender::class); } public function race(): BelongsTo { return $this->belongsTo(Race::class); } public function religion(): BelongsTo { return $this->belongsTo(Religion::class); } } class Gender extends Anything { } class Race extends Anything { } class Religion extends Anything { }
并且您仍然可以通过 Eloquent 关系正常访问
$person = Person::factory()->create(); $person->gender->label; // Ex.: Female $person->race->label; // Ex.: Black $person->religion->label; // Ex.: Atheist
更新日志
请参阅 CHANGELOG 了解最近更改的详细信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全漏洞
请参阅 我们的安全策略 了解如何报告安全漏洞。
致谢
许可证
MIT 许可证(MIT)。请参阅 许可证文件 了解更多信息。