创建表的方式有两种,一种是通过代码层面来执行创建表的操作,一种是使用migration的方式使用artisan命令来执行创建表的操作
下面先提共一个通过代码来创建表的 demo,创建或者指定一个controller,添加一个 createTable的方法,方法调用的结果通过 demo.blade.php 的视图文件展示,如下图:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
public function createMyTable()
{
$tableName = "my_demo_examples";
if (Schema::hasTable($tableName)) {
// 表存在
return view('demo',['MyWords' => 'Table '.$tableName.' exist']);
} else {
// 表不存在
Schema::create($tableName, function (Blueprint $table){
$table->id('Id');
$table->string('UserName');
$table->integer('Age');
$table->timestamps();
});
return view('demo',['MyWords' => 'Table '.$tableName.' not exist, create done']);
}
}
注:Schema里的方法继承来源于 \vendor\laravel\framework\src\Illuminate\Database\Schema\Builder.php
在视图文件里输出调用的函数的结果展示
<!DOCTYPE html>
<html>
<head>
<meta charset=