yabhq / mint
v1.2
2024-04-21 15:31 UTC
Requires
- php: ^8.2|^8.3
- illuminate/support: ^9|^10|^11
- laravel/legacy-factories: ^1.0
Requires (Dev)
- orchestra/testbench: ^9.0
- phpunit/phpunit: ^10.0
- dev-main
- v1.2
- v1.1.1
- v1.1.0
- v1.0.2
- v1.0.1
- v1.0.0
- v0.20.0
- v0.19.2
- v0.19.1
- v0.19
- v0.18.1
- v0.18.0
- v0.17.0
- v0.16.0
- v0.15.0
- v0.14.0
- v0.13.0
- v0.12.0
- v0.11.0
- v0.10.0
- v0.9.0
- v0.8
- v0.7
- v0.6
- v0.5
- v0.4
- v0.3
- v0.2
- v0.1
- dev-laravel-11-support
- dev-dependabot/composer/guzzlehttp/psr7-2.5.0
- dev-feat/composer-updates
This package is auto-updated.
Last update: 2024-04-21 15:32:07 UTC
README
Mint for Laravel
一系列特性、宏和其他辅助函数,以保持您的Laravel应用保持新鲜感。
安装
composer require yabhq/laravel-mint
可存档
允许根据数据库表上的 "archived_at" 字段将模型存档或取消存档。全局作用域在查询模型时会自动排除存档记录。
use Yab\Mint\Traits\Archivable; class Example extends Model { use Archivable; }
$example->archive(); $example->unarchive(); Example::query(); // Will exclude archived items... Example::withArchived(); // With archived items included...
不可变
标记为 "不可变" 的模型如果在更新或删除时(但不是创建时)将抛出 ImmutableDataException。
use Yab\Mint\Traits\Immutable; class Example extends Model { use Immutable; }
// No problem $example = Example::create([ 'field' => 'value' ]); // Throws an exception... $example->update([ 'field' => 'updated' ]);
您还可以通过覆盖模型上的 isImmutable() 函数来自定义模型不可变的条件。
public function isImmutable() { return $this->status === 'closed'; }
UUID 模型
通过利用 UuidModel 特性,轻松使用模型的主键的 UUID。
use Yab\Mint\Traits\UuidModel; class Example extends Model { use UuidModel; }
如果您想自定义 UUID 列的名称,只需在模型类中添加 getUuidColumnName 函数。
public static function getUuidColumnName(): string { return 'my_column_name'; }
货币类型转换
一种自定义转换类型,将货币值存储为数据库中的分,在检索时作为小数值。
use Yab\Mint\Casts\Money; class Example extends Model { protected $casts = [ 'price' => Money::class, ]; }
生成短标题
创建独特且永远不会相互冲突的短标题。
use Yab\Mint\Trails\Slugify; class Example extends Model { use Slugify }
默认情况下,Slugify 特性使用模型上的 name 属性。您可以通过在模型上重写 getSlugKeyName 方法来更改此行为。
public static function getSlugKeyName(): string { return 'title'; }
头像
HasAvatar 特性允许您轻松为用户支持头像,甚至内置了 Gravatar 备用功能!
use Yab\Mint\Trails\HasAvatar; class User extends Model { use HasAvatar; }
您可以自定义用于检索个人资料的数据库字段。
public function getAvatarField() : string { return 'profile_picture'; }
也可以确定用于 Gravatar 电子邮件地址的字段。
public function getEmailField(): string { return 'email'; }