spatie / laravel-model-info
获取您的Laravel应用程序中模型的详细信息
2.0.3
2024-07-16 08:11 UTC
Requires
- php: ^8.2
- illuminate/database: ^11.0
- spatie/laravel-package-tools: ^1.13.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^8.0
- orchestra/testbench: ^9.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-laravel: ^2.3
- spatie/pest-plugin-snapshots: ^2.1
- symfony/serializer: ^6|^7.0
README
使用此包,您可以确定您的模型类具有哪些属性和关系。
use Spatie\ModelInfo\ModelInfo; $modelInfo = ModelInfo::forModel(YourModel::class); $modelInfo->fileName; // returns the filename that contains your model $modelInfo->tableName; // returns the name of the table your models are stored in $modelInfo->attributes; // returns a collection of `Attribute` objects $modelInfo->relations; // returns a collection of `Relation` objects
以下是获取属性信息的方法
$modelInfo->attributes->first()->name; // returns the name of the first attribute $modelInfo->attributes->first()->type; // returns the type of the first attribute (string, integer, ...)
以下是获取关系信息的方法
// returns the name of the first relation, eg. `author` $modelInfo->relations->first()->name; // returns the type of the // first relation, eg. `BelongsTo` $modelInfo->relations->first()->type; // returns the related model of the // first relation, eg. `App\Models\User` $modelInfo->relations->first()->related;
此外,此包还可以发现您应用程序中的所有模型。
use Spatie\ModelInfo\ModelFinder; // returns a `Illuminate\Support\Collection` containing all // the class names of all your models. $models = ModelFinder::all();
支持我们
我们在创建最佳开源包方面投入了大量资源。您可以通过购买我们的付费产品之一来支持我们。
我们非常感谢您从家乡寄给我们明信片,说明您正在使用我们的哪个包。您可以在我们的联系页面上找到我们的地址。我们将所有收到的明信片发布在我们的虚拟明信片墙上。
安装
您可以通过composer安装此包
composer require spatie/laravel-model-info
用法
您可以通过调用forModel
来获取模型信息
use Spatie\ModelInfo\ModelInfo; $modelInfo = ModelInfo::forModel(YourModel::class); $modelInfo->fileName; // returns the filename that contains your model $modelInfo->tableName; // returns the name of the table your models are stored in $modelInfo->attributes; // returns a collection of `Attribute` objects $modelInfo->relations; // returns a collection of `Relation` objects
注意
此包通过返回类型发现关系。请确保在您的所有模型中,每个返回关系的函数都有一个返回类型。
获取特定属性
use Spatie\ModelInfo\ModelInfo; // returns an instance of `Spatie\ModelInfo\Attributes\Attribute` ModelInfo::forModel(BlogPost::class)->attribute('name');
获取特定关系
您可以使用relation
方法获取特定关系。
use Spatie\ModelInfo\ModelInfo; // returns an instance of `Spatie\ModelInfo\Relations\Relation` ModelInfo::forModel(BlogPost::class)->relation('user')
属性
Spatie\ModelInfo\Attributes\Attribute
对象具有以下属性
名称
类型
自增
可为空
默认值
唯一
可填充
附加
类型转换
虚拟
关系
Spatie\ModelInfo\Relations\Relation
对象具有以下属性
名称
类型
相关
它还有一个relatedModelInfo()
方法,该方法为相关模型提供ModelInfo
实例。
use Spatie\ModelInfo\ModelInfo; ModelInfo::forModel(BlogPost::class) ->relation('user') ->relatedModelInfo() // returns the `ModelInfo` for the `User` model
发现您应用程序中的所有模型
使用此方法,我们将发现您项目中的所有方法,无论它们存储在哪个目录中。
use Spatie\ModelInfo\ModelFinder; // returns a `Illuminate\Support\Collection` containing // all the class names of all your models. $models = ModelFinder::all();
获取您应用程序中所有模型的信息
ModelInfo
类可以获取您应用程序中所有模型的信息。
use Spatie\ModelInfo\ModelInfo; ModelInfo::forAllModels(); // returns a collection of `ModelInfo` instances
在模型上添加额外信息
要在模型上添加额外信息,请将extraModelInfo
方法添加到您的模型中。它可以返回任何您想要的内容:字符串、对象、数组。
// in your model public function extraModelInfo() { return 'anything you want'; }
返回值将在ModelInfo
实例的extra
属性中可用。
use Spatie\ModelInfo\ModelInfo; $modelInfo = ModelInfo::forModel(YourModel::class); $modelInfo->extra; // returns 'anything you want'
测试
composer test
更新日志
有关最近更改的更多信息,请参阅更新日志。
贡献
有关详细信息,请参阅贡献指南。
安全漏洞
有关报告安全漏洞的详细信息,请参阅我们的安全策略。
鸣谢
此包包含从Laravel的model:show
命令中获取的代码。
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件。