laravel - 有没有可能从 Laravel 中的模型生成数据库的方法?
问题描述
有没有可能从 Laravel 中的模型生成数据库的方法?我正在开发一个现有的 laravel 项目,它包含没有迁移的模型。
我也尝试了依赖/ laravel 的学说,但没有什么是行不通的..有什么帮助吗?
解决方案
假设您有User
没有迁移的模型,那么您可以从中获取表名
User::getTableName();
有相同的函数来获取列名,你可以从中获取列类型protected $cast = []
这是示例代码
$location = new LocationTable(); // load your model
$tableName = $location->getTable(); // this will give a table name from your model
$columnsWithType = $location->getCasts(); // set all your table fields as cast
Schema::create(tableName , function (Blueprint $table) use ($columnsWithType) {
foreach ($columnsWithType as $columnName => $type) {
$table->$type($columnName);
}
});
推荐阅读
- c - 如何在 C 中导出用户定义的库
- sql - Oracle SQL:声明一个变量并在其他查询中使用该变量
- powershell - Powershell - 在管道内使用 $_ ($PSItem) 的问题是自己的 $_
- c# - C# 重用我的接口进行数据库访问
- google-sheets - Google 表格不允许您更改附加分析的时间表
- python - 使用 Python,如何将 FTP 服务器上的子目录中的多个文件下载到本地机器上所需的目录中?
- angularjs - 农业网格中的复选框选择不起作用
- angular - 强制 mat-expansion-panel 溢出容器元素
- javascript - 如何在加载 Javascript 时设置元素值和运行函数
- discord.net - Discord.net 在多个物理服务器上为同一个机器人运行单独的分片