onursimsek / laravel-extended
这是我的laravel-extended包
1.2.1
2024-09-19 22:21 UTC
Requires
- php: ^8.2|^8.3
- illuminate/contracts: ^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
README
使用混入和更多功能扩展您的Laravel项目
安装
您可以通过composer安装此包
composer require onursimsek/laravel-extended
内容
用法
扩展 Illuminate\Database\Query\Builder
Product::whereGreaterThan('price', 500)->get(); // select * from products where price > 500 Product::whereGreaterThanOrEqual('price', 500)->get(); // select * from products where price >= 500 Product::whereLessThan('price', 500)->get(); // select * from products where price < 500 Product::whereLessThanOrEqual('price', 500)->get(); // select * from products where price <= 500 Product::whereColumnGreaterThan('price', 'amount')->get(); // select * from products where price > amount Product::whereColumnGreaterThanOrEqual('price', 'amount')->get(); // select * from products where price >= amount Product::whereColumnLessThan('price', 'amount')->get(); // select * from products where price < amount Product::whereColumnLessThanOrEqual('price', 'amount')->get(); // select * from products where price <= amount Product::whenWhere(false, 'is_active')->get(); // select * from products Product::whenWhere(true, 'is_active')->get(); // select * from products where is_active = 1
扩展 Illuminate\Support\Str
use Illuminate\Support\Str; Str::squishBetween("I\twill kiss\t\nyou!", 'kiss', 'you'); // I will kiss you! Str::replaceBetween('I will kiss you!', 'will', 'you', 'miss'); // I will miss you! Str::replaceBetweenMatch('I will kiss you!', 'will', 'you', '/k(.*)s/', 'hug'); // I will hug you!
扩展 Illuminate\Support\Stringable
use Illuminate\Support\Str; Str::of("I\twill kiss\t\nyou!")->squishBetween('kiss', 'you'); // I will kiss you! Str::of('I will kiss you!')->replaceBetween('will', 'you', 'miss'); // I will miss you! Str::of('I will kiss you!')->replaceBetweenMatch('will', 'you', '/k(.*)s/', 'hug'); // I will hug you!
有用的特性
与数据库交互
此特性提供了一种管理多个数据库连接之间数据库事务的简便方法。它允许您进行 开始、提交 和 回滚 事务。
beginTransaction(string ...$connections): void
此方法在指定的数据库连接上开始事务。如果没有提供连接,将使用您在Laravel配置中指定的默认数据库连接。
$this->beginTransaction(); // Starts transaction on default connection $this->beginTransaction('mysql', 'sqlite'); // Starts transactions on the 'mysql' and 'sqlite' connections
commit(string ...$connections): void
此方法在指定的连接上提交事务。如果没有指定连接,则不会执行任何操作。
$this->commit(); // No action taken (no specific connection provided) $this->commit('mysql', 'sqlite'); // Commits the transactions on the 'mysql' and 'sqlite' connections
commitAll(): void
此方法提交在对象生命周期内开始的所有连接的事务。
$this->commitAll(); // Commits all active transactions
rollBack(string ...$connections): void
此方法在指定的连接上回滚事务。
$this->rollBack(); // Rolls back on the default connection $this->rollBack('mysql', 'sqlite'); // Rolls back on the 'mysql' and 'sqlite' connections
rollBackAll(): void
此方法回滚在对象生命周期内开始的所有连接的事务。
$this->rollBackAll(); // Rolls back all active transactions
与数据库交互示例
namespace App\Http\Controllers\Controller; use OnurSimsek\LaravelExtended\Support\InteractsWithDatabase; class Controller { use InteractsWithDatabase; public function store() { $this->beginTransaction('mysql', 'pgsql'); try { // Your data processing logic $this->commitAll(); // Commit the transactions if everything goes well } catch (\Exception $e) { $this->rollBackAll(); // Roll back if an error occurs throw $e; } } }
具有名称
此特性将UnitEnum的names转换为数组。
names()
enum Status { use HasName; case Active; case Inactive; } Status::names(); // ['Active', 'Inactive']
具有值
values()和names()
此特性将BackedEnum的names和values转换为数组
enum Status: string { use HasValue; case Active = 'active'; case Inactive = 'inactive'; } Status::names(); // ['Active', 'Inactive'] Status::values(); // ['active', 'inactive']
测试
composer test
变更日志
有关最近更改的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献指南。
安全漏洞
有关如何报告安全漏洞的详细信息,请参阅我们的安全策略。
鸣谢
许可
MIT许可证(MIT)。有关更多信息,请参阅许可文件。