laracraft-tech / laravel-useful-additions
一组有用的 Laravel 扩展!
Requires
- php: ^8.0
- illuminate/contracts: ^9.0 || ^10.0 || ^11.0
- illuminate/database: ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^9.0 || ^10.0 || ^11.0
- illuminate/testing: ^9.0 || ^10.0 || ^11.0
- spatie/laravel-package-tools: ^1.12 || ^1.14
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.13
- nunomaduro/larastan: ^1.0 || ^2.5
- orchestra/testbench: ^7.0 || ^8.0 || ^9.0
- pestphp/pest: ^1.22 || ^2.0
- pestphp/pest-plugin-laravel: ^1.22 || ^2.0
- spatie/laravel-ray: ^1.32
README
在这里,我们将分享一些我们在日常工作中需要的有用的 Laravel 扩展。
特质
命令
安装
您可以通过 composer 安装此包
composer require laracraft-tech/laravel-useful-additions
然后使用以下命令发布配置文件
php artisan vendor:publish --tag="useful-additions-config"
特质
有用的枚举
此特质仅在安装了 PHP 8.1 或更高版本时可用。
names
, values
, array
如果您喜欢遍历所有的枚举类型,或者您可能想将枚举用作数组,例如在迁移中,这将非常有用。
use LaracraftTech\LaravelUsefulAdditions\Traits\UsefulEnums; enum PaymentType: int { use UsefulEnums; case Pending = 1; case Failed = 2; case Success = 3; } PaymentType::names(); // return ['Pending', 'Failed', 'Success'] PaymentType::values(); // return [1, 2, 3] PaymentType::array(); // return ['Pending' => 1, 'Failed' => 2, 'Success' => 3]
有用的作用域
selectAllBut
选择除给定排除数组之外的列。
use LaracraftTech\LaravelUsefulAdditions\Traits\UsefulScopes; $class = new class extends Model { use UsefulScopes; protected $timestamps = false; protected $table = 'scope_tests'; }; $class->create([ 'foo' => 'foo', 'bar' => 'bar', 'quz' => 'quz', ]); $class::query()->selectAllBut(['foo'])->first()->toArray(); // return ['bar' => 'bar', 'quz' => 'quz']
注意: 由于您不能在 mysql 中做原生的 "select all but x,y,z",我们需要查询(并缓存)表中现有的列,然后排除应被 忽略(不选择)的给定列。
缓存: 每个表的列名将被缓存,直到迁移目录的内容被添加或删除。修改迁移目录内文件的内容不会重新缓存列。在您进行新的 部署/迁移 时,请考虑清除缓存!
fromToday
, fromYesterday
选择今天或昨天创建的所有条目。
use LaracraftTech\LaravelUsefulAdditions\Traits\UsefulScopes; $class = new class extends Model { use UsefulScopes; protected $timestamps = true; protected $table = 'scope_tests'; }; $class->create(['foo' => 'foo1', 'bar' => 'bar1', 'quz' => 'quz1']); $class->create(['foo' => 'foo2', 'bar' => 'bar2', 'quz' => 'quz2', 'created_at' => now()->yesterday()]); $class::select('foo')->fromToday()->first()->toArray(); // return ['foo' => 'foo1'] $class::select('foo')->fromYesterday()->first()->toArray(); // return ['foo' => 'foo2']
RefreshDatabaseFast
这是一个特质,它使测试套件中的数据库迁移变得非常、非常快!基本思路来自 Mayahi。它基本上只有在迁移文件 已更改 时才迁移数据库。所以第一次 migrate:fresh
可能需要一段时间(取决于您有多少迁移),然后它就非常快了。
如果愿意,可以将 USEFUL_ADDITIONS_SEED_AFTER_FAST_DB_REFRESH
设置为 true
以在迁移后对数据库进行播种。
请确保将 .phpunit.database.checksum
添加到您的 .gitignore
文件中!
Pest
use LaracraftTech\LaravelUsefulAdditions\Traits\RefreshDatabaseFast; uses(RefreshDatabaseFast::class); it('does_something', function() { // ... });
PHPUnit
use LaracraftTech\LaravelUsefulAdditions\Traits\RefreshDatabaseFast; use Tests\TestCase; class MyTest extends TestCase { use RefreshDatabaseFast; /** @test **/ public function it_does_something() { // ... } }
命令
db:truncate
此命令将截断当前数据库连接下的所有表。查看 --help
了解参数和选项。例如,它还允许您只截断特定的表,或禁用 外键检查,或以 强制 模式运行。
php artisan db:truncate
INFO Start truncating tables.
Truncating table: failed_jobs .............................................. 135ms DONE
Truncating table: migrations ................................................ 87ms DONE
Truncating table: password_reset_tokens ..................................... 79ms DONE
Truncating table: personal_access_tokens .................................... 86ms DONE
Truncating table: users ..................................................... 78ms DONE
INFO Finished truncating tables.
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全漏洞
请查看 我们的安全策略 了解如何报告安全漏洞。
鸣谢
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。