laracraft-tech/laravel-useful-additions 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

laracraft-tech/laravel-useful-additions

Composer 安装命令:

composer require laracraft-tech/laravel-useful-additions

包简介

A collection of useful Laravel additions!

README 文档

README

Latest Version on Packagist Tests Check & fix styling License Total Downloads Laravel Compatibility

Here we will share some useful Laravel additions we need in our daily work.

Traits

Commands

Installation

You can install the package via composer:

composer require laracraft-tech/laravel-useful-additions

Then publish the config file with:

php artisan vendor:publish --tag="useful-additions-config"

Traits

UsefulEnums

This trait is only available with PHP 8.1 or higher installed.

names, values, array

This could be very handy if you like to loop over all of your enum types, or you maybe want to use the enum as an array, for instance in a migration.

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]

UsefulScopes

selectAllBut

Select all columns but given excluded array.

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']

Note: Since you can't do a native "select all but x,y,z" in mysql, we need to query (and cache) the existing columns of the table, and then exclude the given columns which should be ignored (not selected) from the existing columns.

Cache: Column names of each table will be cached until contents of migrations directory is added or deleted. Modifying the contents of files inside the migrations directory will not re-cache the columns. Consider to clear the cache whenever you make a new deployment/migration!

fromToday, fromYesterday

Select all entries created today or yesterday.

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

Deprecated: We recommend using Laravel's built-in RefreshDatabase trait instead.

Laravel now uses a similar approach internally (migrate once, then rollback per test), which makes this trait largely redundant. Additionally, RefreshDatabaseFast can cause persistent data across tests when a test is aborted unexpectedly, because the rollback may not be triggered. The small performance gain of skipping the initial migration via checksum is minimal compared to the potential issues this can cause.

Use Illuminate\Foundation\Testing\RefreshDatabase for a more resilient testing experience.

This is a trait which makes the migration of your database in your test suite much, much faster! The base idea comes from Mayahi. It basically only migrates your database if the migration files has changed. So the first migrate:fresh takes a while (depending on how many migrations you have), and then it's incredible fast.

Optionally you can set USEFUL_ADDITIONS_SEED_AFTER_FAST_DB_REFRESH to true if you like to seed your database after the migration.

Also make sure to add the .phpunit.database.checksum to your .gitignore file!

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()
    {
        // ...
    }
}

Commands

db:truncate

This command truncates all the tables of your current database connection. Checkout --help to see arguments and options. It for instance, lets you also truncate only specific tables or disable foreigen key checks or maybe run in force mode.

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.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

laracraft-tech/laravel-useful-additions 适用场景与选型建议

laracraft-tech/laravel-useful-additions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 125.8k 次下载、GitHub Stars 达 58, 最近一次更新时间为 2023 年 03 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「laracraft-tech」 「laravel-useful-additions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 laracraft-tech/laravel-useful-additions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 laracraft-tech/laravel-useful-additions 我们能提供哪些服务?
定制开发 / 二次开发

基于 laracraft-tech/laravel-useful-additions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 125.8k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 58
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 58
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-12