根据用户输入在Laravel中创建新的表格? [英] Creating new table in Laravel based on user input?

查看:144
本文介绍了根据用户输入在Laravel中创建新的表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



用户提交在控制器中处理的表单,表单数据被保存,然后根据用户输入创建一个新表(以便稍后接收数据)。



可以使用schema :: create()

 模式:: create('newtablename',function($ table){
$ table-> ;增量('id') - > unique(); //主键
$ table-> string('columnname');

});

本质上我想这样做,除了基于用户输入的表名和列名: / p>

用户输入是一个这样的多维数组:

  $ newtableschema =数组(
'tablename'=>'tablename',
'colnames'=>数组('One','Two','Three','Four');
) ;

Schema :: create($ newtableschema ['tablename'],function($ table){
$ table-> incremental('id') - > unique() /主键

foreach($ newtableschema ['colnames'] as $ col){
$ table-> text($ col);
}
etc 。
});

我得到的错误是未定义变量。我不知道如何给用户输入schema :: create方法。



我在文档中找不到任何方法来创建这样的桌子,或者在我过去3个小时的谷歌搜索中的任何类似的东西。雄辩的语法有时会混淆我,所以也许我错过了一些明显的东西。



任何想法?还是有一个替代的纯php / sql来完成这个?



非常感谢



编辑:我的道歉,我在我的示例代码中提到了一个错字。我的问题是我不知道如何传递我想要的模式:: create()

解决方案

你可以使用 use($ newtableschema)将变量传递给闭包

  Schema :: create($ newtableschema ['tablename'],function($ table)use($ newtableschema){

// so now你可以访问$ newtableschema变量
//其余的代码...
});


I am trying to create new mysql tables based on user input in the Laravel Framework.

A user submits a form that is processed in a controller, the form data is saved, and then a NEW table is created based on the user's input (to receive data at a later time).

In laravel tables can be created using schema::create()

Schema::create('newtablename', function($table){
    $table->increments('id')->unique(); //primary key
    $table->string('columnname');
    etc.
});

Essentially I want to do this, except basing the table name and column names on the user's input:

User input is a multidimensional array like this:

    $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.
    });

The error I get is that the variables is not defined. I don't know how to give the user's input to the schema::create method.

I can't find any method in the documentation for creating tables in this way, or anything similar to this during my last 3 hours of googling. The eloquent syntax confuses me sometimes so maybe I'm missing something obvious.

Any ideas? Or is there an alternative pure php/sql to accomplish this?

Many thanks

edit: my apologies, I made a typo earlier in my example code. My issue is that I don't know how to pass the variables I want to schema::create()

解决方案

You may use use($newtableschema) to pass the variable into the closure:

Schema::create($newtableschema['tablename'], function($table) use($newtableschema) {

    // So now you can access the $newtableschema variable here
    // Rest of your code...
});

这篇关于根据用户输入在Laravel中创建新的表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆