Laravel - Creating tables dynamically (without migration)

大城市里の小女人 提交于 2019-12-04 17:02:23

问题


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!


回答1:


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');
});

It is

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

Hope this helps someone someday in the future!




回答2:


This one is even better.

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

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


来源:https://stackoverflow.com/questions/30096105/laravel-creating-tables-dynamically-without-migration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!