了解LaravelModel Info软件包

199 阅读1分钟

LaravelModel Info是一个由Spatie制作的软件包,用于获取Laravel项目中所有模型的信息。如果你正在构建需要以编程方式检查模型的功能,这个包会很有帮助。

例如, 你可以访问许多重要的细节, 如数据库表名, 属性, 关系, 和更多。

use Spatie\ModelInfo\ModelInfo;
 
$model = ModelInfo::for(Post::class);
$model->attributes;
$model->relations;
// etc.
 
// Attributes and relations are collections
$model->attributes->first()->name; // title
$model->attributes->first()->type; // string(255)
$model->attributes->first()->phpType; // string

我注意到这个包的一个奇妙的功能是获得你项目中的所有模型。

// Returns a collection of all your app's models
$models = ModelFinder::all();