Laravel 模型信息 是 Spatie 的一个包,用于获取有关 Laravel 中所有模型的信息项目。如果你正在构建需要以编程方式检查模型的功能,此包会很有帮助。
例如,你可以访问许多重要的详细信息,如数据库表名、属性、关系等:
use Spatie\ModelInfo\ModelInfo;
$model = ModelInfo::for(Post::class);
$model->attributes;
$model->relations;
// etc.
// 属性和关系是集合
$model->attributes->first()->name; // title
$model->attributes->first()->type; // string(255)
$model->attributes->first()->phpType; // string
我在这个包中注意到的一个很棒的功能是获取项目中的所有模型:
// 返回所有应用模型的集合
$models = ModelFinder::all();
要了解更多信息,请查看 Freek Van der Herten 的文 Getting information about all the models in your Laravel app。 你可以在 spatie/laravel-model-info的 GitHub 上获取文档和源代码。
原文地址:https://laravel-news.com/laravel-model-info
译文地址:https://learnku.com/laravel/t/71822
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)