vicgutt / laravel-inspect-db
检查并检索指定数据库的信息
Requires
- php: ^8.1
- doctrine/dbal: ^3.4
- illuminate/contracts: ^10.0
- spatie/laravel-package-tools: ^1.9.2
- vicgutt/php-enhanced-enum: ^0.1.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/invade: ^1.1
README
此包允许您检查并检索数据库信息。此包已针对 Sqlite、MySQL 和 PostgreSQL 进行了测试,尽管其他数据库也可能适用。如果不适用,请讨论。
以下是一个快速示例
use VicGutt\InspectDb\Inspect; // On a default Laravel "users" table using the "mysql" connection, running: Inspect::table($name = 'users', $connectionOrSchemaManagerOrNull = 'mysql')->toArray(); // would return the following: [ 'name' => 'users', 'engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci', 'charset' => 'utf8mb4', 'autoincrement' => 1, 'comment' => '', 'primaryKey' => [ 'name' => 'PRIMARY', 'primary' => true, 'unique' => true, // ... ], 'columns' => [ 'id' => [/* ... */], 'name' => [/* ... */], 'email' => [/* ... */], // ... ], 'indexes' => [/* ... */], 'foreignKeys' => [], ]
安装
您可以使用 composer 安装此包
composer require vicgutt/laravel-inspect-db
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="laravel-inspect-db-config"
您可以在以下链接中查看发布配置文件的内容: config/inspect-db.php
检查
VicGutt\InspectDb\Inspect
类是此包的主要入口点。它允许您检索有关表、表列、索引和外键的信息。
检索特定数据库连接的表
use VicGutt\InspectDb\Inspect; // returns an instance of `VicGutt\InspectDb\Collections\Entities\TableCollection` Inspect::tables();
在此处,由于未指定数据库连接,将使用默认配置的数据库连接。
检索特定数据库连接的特定表
use VicGutt\InspectDb\Inspect; // returns an instance of `VicGutt\InspectDb\Entities\Table` Inspect::table('users');
在此处,由于未指定第二个参数作为数据库连接,将使用默认配置的数据库连接。
检索特定数据库连接的特定表的列
use VicGutt\InspectDb\Inspect; // returns an instance of `VicGutt\InspectDb\Collections\Entities\ColumnCollection` Inspect::columns('users');
在此处,由于未指定第二个参数作为数据库连接,将使用默认配置的数据库连接。
检索特定数据库连接的特定表的索引
use VicGutt\InspectDb\Inspect; // returns an instance of `VicGutt\InspectDb\Collections\Entities\IndexCollection` Inspect::indexes('users');
在此处,由于未指定第二个参数作为数据库连接,将使用默认配置的数据库连接。
检索特定数据库连接的特定表的外键
use VicGutt\InspectDb\Inspect; // returns an instance of `VicGutt\InspectDb\Collections\Entities\ForeignKeyCollection` Inspect::foreignKeys('users');
在此处,由于未指定第二个参数作为数据库连接,将使用默认配置的数据库连接。
检索特定数据库连接的特定表的特定列
use VicGutt\InspectDb\Inspect; // returns an instance of `VicGutt\InspectDb\Entities\Column` or null Inspect::column('id', 'users');
在此处,由于未指定第三个参数作为数据库连接,将使用默认配置的数据库连接。
检索特定数据库连接的特定表的特定索引
use VicGutt\InspectDb\Inspect; // returns an instance of `VicGutt\InspectDb\Entities\Index` or null Inspect::index('PRIMARY', 'users');
在此处,由于未指定第三个参数作为数据库连接,将使用默认配置的数据库连接。
检索特定数据库连接的特定表的外键
use VicGutt\InspectDb\Inspect; // returns an instance of `VicGutt\InspectDb\Entities\ForeignKey` or null Inspect::foreignKey('posts_user_id_foreign', 'posts');
在此处,由于未指定第三个参数作为数据库连接,将使用默认配置的数据库连接。
集合
此包提供的所有集合都扩展了抽象类 VicGutt\InspectDb\Collections\Entities\EntityCollection
,该类本身又从默认的 Laravel 集合 (Illuminate\Support\Collection
) 继承而来。
可用的集合有
- VicGutt\InspectDb\Collections\Entities\
TableCollection
- VicGutt\InspectDb\Collections\Entities\
ColumnCollection
- VicGutt\InspectDb\Collections\Entities\
IndexCollection
- VicGutt\InspectDb\Collections\Entities\
ForeignKeyCollection
上述集合与您可能习惯的默认 Laravel 集合和行为略有不同。也就是说,我们集合的内部项只能是表示其实体的数组。例如,TableCollection
的项只能是 Table
的数组。
在用法中,这会导致在使用某些集合方法时抛出类型错误
/** * The following will throw a `TypeError` with a message specifying the "$item" given * is a string rather than instances of `Doctrine\DBAL\Schema\Table` or `VicGutt\InspectDb\Entities\Table`. */ Inspect::tables()->map(fn (Table $table): string => $table->name); Inspect::tables()->pluck('name');
解决方法是,在调用返回当前集合的新实例但具有变异项的方法之前,将我们的集合转换为默认 Laravel 集合
/** * Now, all is well. */ Inspect::tables()->toBase()->map(fn (Table $table): string => $table->name); Inspect::tables()->toBase()->pluck('name');
虽然这种做法可能令人惊讶,但它有助于确保只包含 X 的集合实际上只包含 X。
实体
实体旨在表示给定数据库中的单元。
可用的实体有
- VicGutt\InspectDb\Entities\
Table
- VicGutt\InspectDb\Entities\
Column
- VicGutt\InspectDb\Entities\
Index
- VicGutt\InspectDb\Entities\
ForeignKey
点击上述列表中的任何实体,了解有关公开属性和方法的更多信息。
测试
composer test
变更日志
请参阅变更日志获取最近更改的更多信息。
贡献
如果您有兴趣为项目做出贡献,请在提交拉取请求之前阅读我们的贡献文档 。
安全漏洞
请查阅我们关于如何报告安全漏洞的安全策略。
鸣谢
许可协议
MIT 许可协议 (MIT)。有关更多信息,请参阅许可文件。