Laravel-动态创建表(无需迁移) [英] Laravel - Creating tables dynamically (without migration)

查看:58
本文介绍了Laravel-动态创建表(无需迁移)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据管理员请求动态创建一个表,尽管看起来像Laravel的大多数文档中一样有趣且花哨,但我似乎无法创建.尽管我可以删除表,并根据需要添加或删除列.

I'm trying to create a table dynamically upon an admin request, and while it seems all fun and dandy like in most of Laravel's documentation, I can't seem to create a table. Though I can drop tables, and add or drop columns as I wish.

这是我的基本代码模型:

This is my basic code model:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

public static function createTable() {

    Schema::create('randomName', function($table)
    {
        $table->increments('id');
        $table->string('name');
    });
}

这里可能是什么问题?不幸的是我没有收到任何错误,所以我不知道如何调试它.就像一切正常一样,它继续进行到下一行.它只是不创建任何表.

What could possibly be the problem here? Unfortunately I don't receive any errors, so I don't know how to debug it. It just goes on to the next line, like everything's working fine. It just doesn't create any tables.

有什么建议吗?提前非常感谢!

Any advice? Much thanks in advance!

推荐答案

哎呀,没关系...我已经研究了足够长时间,而且解决方案一如既往...非常简单.

Ugh, never mind... I worked on it for long enough, and the solution as always was... Very simple.

我只需要首先为数据库找到一个连接,所以代替

I just had to figure a connection for the database first, so instead of

Schema::create('tableName', function($table)
{           
    $table->increments('id');
});

Schema::connection('mysql')->create('tableName', function($table)
{
    $table->increments('id');
});

希望这对以后的某人有帮助!

Hope this helps someone someday in the future!

这篇关于Laravel-动态创建表(无需迁移)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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