如何通过Laravel的migrate命令高效创建所需的数据表?
- 内容介绍
- 文章标签
- 相关推荐
本文共计795个文字,预计阅读时间需要4分钟。
Laravel 中,可以使用 migration 创建数据表,这极大地方便了数据库的迁移。以下是在 Laravel 5.5+ 版本中,使用 migration 创建数据表的步骤:
1. 创建迁移文件
2.定义数据表结构
3.运行迁移
步骤详解:
1. 创建迁移文件:
使用 Artisan 命令创建一个新的迁移文件。 bash php artisan make:migration create_users_table 这将在 `database/migrations` 目录下创建一个名为 `create_users_table.php` 的文件。2. 定义数据表结构: 打开 `database/migrations/create_users_table.php` 文件,并修改内容如下: php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration { public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); }
public function down() { Schema::dropIfExists('users'); } }
3. 运行迁移: 使用 Artisan 命令运行迁移,将迁移文件中的数据表结构应用到数据库中。 bash php artisan migrate
现在,您已经成功在 Laravel 中使用 migration 创建了一个名为 `users` 的数据表。
laravel中可以使用migration创建数据表,这使得数据库的迁移非常便利,下面介绍一下laravel中使用migration创建数据表的过程。数据库使用的是mysql,laravel版本为5.5
1. 创建并连接数据库
创建数据库
在命令行中输入mysql -u root -p然后输入数据库密码,
创建数据库create database work_space,
回车完成数据库的创建
连接数据库
打开项目中的.env文件
APP_NAME=Laravel APP_ENV=local APP_KEY=base64:kFEhG73pi95EeRVeveIfo11Q0bSui/4Y2tKvjiT0zFc= APP_DEBUG=true APP_LOG_LEVEL=debug APP_URL=localhost DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=work_space //数据库名 DB_USERNAME=root //用户名 DB_PASSWORD=root //密码 BROADCAST_DRIVER=log CACHE_DRIVER=file SESSION_DRIVER=file SESSION_LIFETIME=120 QUEUE_DRIVER=sync REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379 MAIL_DRIVER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null PUSHER_APP_ID= PUSHER_APP_KEY= PUSHER_APP_SECRET= PUSHER_APP_CLUSTER=mt1
2. 使用migration创建数据表
创建一个migration
打开项目根目录(我的是/var/www/html/work_space/)
输入命令:php artisan make:migration create_table_users
如上则成功创建一个migration,
在database/migrations/ 会发现多了一个名为
2018_07_31_143907_create_table_users.php
打开这个文件,并在up方法中添加要建的表中的字段信息,如下:
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateTableUsers extends Migration { /** * Run the migrations. * * @return void */ public function up() { // 创建用户表 Schema::create('users', function (Blueprint $table) { $table->increments('user_id'); $table->string('user_email',32)->default('')->comment('用户登录名:企业邮箱'); $table->string('user_password',32)->default('')->comment('用户密码,初始值为企业邮箱'); $table->ipAddress('user_ip')->default('')->comment('用户最后一次登录ip'); $table->integer('user_login_cnt')->default(0)->comment('用户登录次数'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { // } }
在命令行中执行php artisan migrate,结果如下(我创建了四张表):
打开数据库,查看有哪些表,show tables结果如下:
以上便完成了使用migration创建数据表,希望能给大家一个参考,也希望大家多多支持易盾网络。
本文共计795个文字,预计阅读时间需要4分钟。
Laravel 中,可以使用 migration 创建数据表,这极大地方便了数据库的迁移。以下是在 Laravel 5.5+ 版本中,使用 migration 创建数据表的步骤:
1. 创建迁移文件
2.定义数据表结构
3.运行迁移
步骤详解:
1. 创建迁移文件:
使用 Artisan 命令创建一个新的迁移文件。 bash php artisan make:migration create_users_table 这将在 `database/migrations` 目录下创建一个名为 `create_users_table.php` 的文件。2. 定义数据表结构: 打开 `database/migrations/create_users_table.php` 文件,并修改内容如下: php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration { public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); }
public function down() { Schema::dropIfExists('users'); } }
3. 运行迁移: 使用 Artisan 命令运行迁移,将迁移文件中的数据表结构应用到数据库中。 bash php artisan migrate
现在,您已经成功在 Laravel 中使用 migration 创建了一个名为 `users` 的数据表。
laravel中可以使用migration创建数据表,这使得数据库的迁移非常便利,下面介绍一下laravel中使用migration创建数据表的过程。数据库使用的是mysql,laravel版本为5.5
1. 创建并连接数据库
创建数据库
在命令行中输入mysql -u root -p然后输入数据库密码,
创建数据库create database work_space,
回车完成数据库的创建
连接数据库
打开项目中的.env文件
APP_NAME=Laravel APP_ENV=local APP_KEY=base64:kFEhG73pi95EeRVeveIfo11Q0bSui/4Y2tKvjiT0zFc= APP_DEBUG=true APP_LOG_LEVEL=debug APP_URL=localhost DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=work_space //数据库名 DB_USERNAME=root //用户名 DB_PASSWORD=root //密码 BROADCAST_DRIVER=log CACHE_DRIVER=file SESSION_DRIVER=file SESSION_LIFETIME=120 QUEUE_DRIVER=sync REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379 MAIL_DRIVER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null PUSHER_APP_ID= PUSHER_APP_KEY= PUSHER_APP_SECRET= PUSHER_APP_CLUSTER=mt1
2. 使用migration创建数据表
创建一个migration
打开项目根目录(我的是/var/www/html/work_space/)
输入命令:php artisan make:migration create_table_users
如上则成功创建一个migration,
在database/migrations/ 会发现多了一个名为
2018_07_31_143907_create_table_users.php
打开这个文件,并在up方法中添加要建的表中的字段信息,如下:
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateTableUsers extends Migration { /** * Run the migrations. * * @return void */ public function up() { // 创建用户表 Schema::create('users', function (Blueprint $table) { $table->increments('user_id'); $table->string('user_email',32)->default('')->comment('用户登录名:企业邮箱'); $table->string('user_password',32)->default('')->comment('用户密码,初始值为企业邮箱'); $table->ipAddress('user_ip')->default('')->comment('用户最后一次登录ip'); $table->integer('user_login_cnt')->default(0)->comment('用户登录次数'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { // } }
在命令行中执行php artisan migrate,结果如下(我创建了四张表):
打开数据库,查看有哪些表,show tables结果如下:
以上便完成了使用migration创建数据表,希望能给大家一个参考,也希望大家多多支持易盾网络。

