首页 > 解决方案 > 有没有可能从 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);
            }
        });

推荐阅读