mll-lab/laravel-utils

MLL 共享的 Laravel 工具

v6.0.0 2024-09-17 11:42 UTC

README

Continuous Integration Code Coverage

Latest Stable Version Total Downloads

MLL 共享的 Laravel 工具

安装

通过 composer 安装

composer require mll-lab/laravel-utils

使用方法

参见 测试

自增

允许在不实际使用自增的情况下创建自增 ID。

扩展类 Autoincrement 并为您的 ID 起一个描述性的名字。

use MLL\LaravelUtils\Database\Autoincrement;

final class MaxFooId extends Autoincrement
{
    public static function name(): string
    {
        return 'max_foo_id';
    }
}

生成迁移并调用其中的 createTable() 方法

public function up() {
    MaxFooId::createTable();
}

要在模型中使用此 ID,将 $incrementing 设置为 false 并在 booted() 方法中将 ID 赋值给模型

public $incrementing = false;

protected static function booted(): void
{
    self::creating(function (self $instance): void {
        $instance->id ??= MaxFooId::next();
    });

条件迁移

要条件性地运行迁移,实现 MLL\LaravelUtils\Database\ConditionalMigration 接口及其 ->shouldRun() 方法

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Carbon;
use MLL\LaravelUtils\Database\ConditionalMigration

return new class extends Migration implements ConditionalMigration {
    public function up(): void
    {
        // Something that would put intense strain on the database
    }

    public function shouldRun(): bool
    {
        $currentHour = Carbon::now()->hour;

        // Only run between 01:00 and 03:00
        return $currentHour > 1 && $currentHour < 3;
    }
};

严格占位符

为了使您的占位符持续更新为该包的最新版本,请将 /stubs 添加到您的 .gitignore 文件中,并将以下内容添加到您的 composer.json 文件中

    "scripts": {
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
+           "@php artisan vendor:publish --tag=strict-stubs --force"
        ]
    }

变更日志

参见 CHANGELOG.md

贡献

参见 CONTRIBUTING.md

许可证

本软件包采用 MIT 许可证授权。