【发布时间】:2014-11-16 17:37:40
【问题描述】:
我正在尝试根据 Laravel 框架中的用户输入创建新的 mysql 表。
用户提交一个在控制器中处理的表单,表单数据被保存,然后根据用户的输入创建一个 NEW 表(以便稍后接收数据)。
在 laravel 中可以使用 schema::create() 创建表
Schema::create('newtablename', function($table){
$table->increments('id')->unique(); //primary key
$table->string('columnname');
etc.
});
基本上我想这样做,除了基于用户输入的表名和列名:
用户输入是这样的多维数组:
$newtableschema = array(
'tablename' => 'tablename',
'colnames' => array('One', 'Two', 'Three', 'Four');
);
Schema::create($newtableschema['tablename'], function($table){
$table->increments('id')->unique(); //primary key
foreach($newtableschema['colnames'] as $col){
$table->text($col);
}
etc.
});
我得到的错误是未定义变量。我不知道如何将用户的输入提供给 schema::create 方法。
在我过去 3 个小时的谷歌搜索中,我在文档中找不到以这种方式创建表的任何方法,或任何类似的方法。雄辩的语法有时让我感到困惑,所以也许我遗漏了一些明显的东西。
有什么想法吗?或者有没有替代的纯 php/sql 来完成这个?
非常感谢
编辑:抱歉,我之前在示例代码中打错了字。我的问题是我不知道如何将我想要的变量传递给 schema::create()
【问题讨论】:
-
您遇到的错误是什么?
$newtableschema['array']是什么?array是字段名还是什么? -
还有
$table->text('$colname');不行,改成$table->text($colname);。 -
抱歉,我在解释代码时打错了字,我已经修改了原帖以使其更清晰。用户提供了一个多维数组,但我不知道如何将它传递给 laravel 中的 create table 方法。
标签: php mysql laravel schema eloquent