phpexperts / postgres-for-laravel
一个易于使用Laravel操作PostgreSQL的库。
v1.1.0
2024-05-26 14:37 UTC
Requires
- php: >=7.3
Requires (Dev)
This package is auto-updated.
Last update: 2024-08-26 15:15:01 UTC
README
Postgres For Laravel 是PHP Experts, Inc.的一个项目,旨在简化在Laravel中使用PostgreSQL数据库。
安装
通过Composer
composer require phpexperts/postgres-for-laravel
使用方法
通过composer引入后,该库即可立即使用。
PostgreSQL 时间戳
PostgreSQL对时间戳的支持优于MySQL。然而,Laravel默认只支持简化版的时间戳。为了最佳性能,包括具有时区和毫秒分辨率的时间戳,最好让PostgreSQL本身处理每个表的时戳。为此,请执行以下操作
vendor | project | created_at | updated_at
------------+----------------------+----------------------------+----------------------------
phpexperts | simple-dto | 2020-04-30 23:35:00 | 2023-07-18 19:08:47
phpexperts | rest-speaker | 2023-07-30 09:35:53 | 2023-07-30 09:37:37
phpexperts | postgres-for-laravel | 2023-12-14 17:58:46.381623 | 2023-12-14 17:58:46.417537
自动自动注入
- 将每个模型扩展为PHPExperts\PostgresForLaravel\PostgresModel。
- 运行
./artisan migrate
手动连接
- 在您的模型中添加
public $timestamps = false;
- 创建一个新的迁移:
./artisan make:migration use_native_postgres_timestamps
- 在迁移中添加以下代码
<?php declare(strict_types=1); use Illuminate\Database\Migrations\Migration; use PHPExperts\PostgresForLaravel\PostgresMigrationHelper; class UseNativePostgresTimestamps extends Migration { private const TABLES = [ 'packages', ]; public function up(): void { PostgresMigrationHelper::addPostgresTimestamps(static::TABLES); } public function down(): void { PostgresMigrationHelper::dropPostgresTimestamps(static::TABLES, PostgresMigrationHelper::DEFAULT_TIMESTAMPS); } }
用例
✔ 使用PostgreSQL原生时间戳生成代码。
测试
phpunit --testdox
贡献者
Theodore R. Smith theodore@phpexperts.pro
GPG指纹:4BF8 2613 1C34 87AC D28F 2AD8 EB24 A91D D612 5690
CEO:PHP Experts, Inc.
许可证
MIT许可证。请参阅许可证文件以获取更多信息。 :wq