Laravel Migrate

artisan命令行创建migrate

格式:

1 php artisan make:migration YourFileName

示例:

1 php artisan make:migration create_books_table

我们找到laravel目录下database\migrations\2017_XX_XX_XXXXXX_create_books_table.php

注意:XX代表时间戳,因时而异


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27


<?php

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class CreateBooksTable extends Migration

{

    /**

     * Run the migrations.

     *

     * @return void

     */

    public function up()

    {

        //

    }

    /**

     * Reverse the migrations.

     *

     * @return void

     */

    public function down()

    {

        //

    }

}

这里的CreateBooksTable类继承了Migration,我们看下Migration类


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19


namespace Illuminate\Database\Migrations;

abstract class Migration

{

    /**

     * The name of the database connection to use.

     *

     * @var string

     */

    protected $connection;

    /**

     * Get the migration connection name.

     *

     * @return string

     */

    public function getConnection()

    {

        return $this->connection;

    }

}

这里我们把up函数改写成


1

2

3

4

5

6

7

8

9

10

11


public function up()

{

    Schema::create(‘chs‘function (Blueprint $table) {

        $table->increments(‘id‘);           

        $table->string(‘slug‘)->unique();//additional

        $table->string(‘title‘);//additional

        $table->text(‘content‘);//additional

        $table->timestamps();

        $table->timestamp(‘published_at‘)->index();//additional

    });

}

同时参考以下文档,文档选自http://laravelbook.com/laravel-migrations-managing-databases/

一切准备就绪,我们开始迁移

1 php artisan migrate

如果你迁移后发觉并不是你想要的数据表,可以回滚

1 php artisan migrate:rollback

这里要给大家提个醒,down方法一定要写,可能有些人这个方法会空着,觉得只要数据表创建出来了就没事了,但是一旦涉及rollback又没有down方法,你的migrate操作就是不可逆的!  

就像这样:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34


<?php

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class CreateCommentsTable extends Migration

{

    /**

     * Run the migrations.

     *

     * @return void

     */

    public function up()

    {

        Schema::create(‘comments‘function (Blueprint $table) {

            $table->increments(‘id‘);           

            $table->integer(‘postid‘);

            $table->string(‘title‘);//additional

            $table->text(‘content‘);//additional

            //$table->timestamps();

            $table->timestamp(‘published_at‘)->index();//additional

        });

    }

    /**

     * Reverse the migrations.

     *

     * @return void

     */

    public function down()

    {

        Schema::drop(‘comments‘);

    }

}

Laravel使用了门面模式


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26


namespace Illuminate\Support\Facades;

/**

 * @see \Illuminate\Database\Schema\Builder

 */

class Schema extends Facade

{

    /**

     * Get a schema builder instance for a connection.

     *

     * @param  string  $name

     * @return \Illuminate\Database\Schema\Builder

     */

    public static function connection($name)

    {

        return static::$app[‘db‘]->connection($name)->getSchemaBuilder();

    }

    /**

     * Get a schema builder instance for the default connection.

     *

     * @return \Illuminate\Database\Schema\Builder

     */

    protected static function getFacadeAccessor()

    {

        return static::$app[‘db‘]->connection()->getSchemaBuilder();

    }

}

时间: 2024-10-11 01:20:05

Laravel Migrate的相关文章

laravel migrate时报错:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

在按照文档执行php artisan migrate时报错. SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`)) 解决方法 namespace App\Providers; use Illum

laravel 执行migrate出现异常

今天在执行laravel migrate时出现异常,找了好半天才找到问题所在,特此记录一下. 配好数据库,执行 ``` php artisan migrate ```.但是遇到了问题: migrations中up函数为: public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->

Laravel 5.4 migrate报错:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `us ers_email_unique`(`email`))

Laravel 5.4 migrate报错:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `us     ers_email_unique`(`email`)) public function up() { Schema::create('users', function (Blu

Laravel 5.4 migrate时报错: Specified key was too long error

Laravel 5.4默认使用utf8mb4字符编码,而不是之前的utf8编码.因此运行php artisan migrate 会出现如下错误: [Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add

laravel项目3myPersimmon学习(使用了什么插件,视图,编辑器,migrate,seeder)1urlencode,sha1_file

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px ".PingFang SC"; color: #454545 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "He

Laravel 5 migrate时报错: Specified key was too long error

Laravel 5.4默认使用utf8mb4字符编码,而不是之前的utf8编码.因此运行php artisan migrate 会出现如下错误: [Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add

laravel数据库迁移的migrate小解

当通过命令行:php artisan migrate:make create_authors_table --table=authors --create时,在 migration.php 中若Schema::table()而不是Schema::create时,会出现 table 不存在的指示,这时需要将 Schema::table 改为 Schema::create . 有时输入:php artisan migrate 会显示其他表已存在导致无法创建新的表,这时可以将旧的 migration.

laravel 5.4 中使用migrate

1. 创建表结构 a. 命令: php artisan make:migration create_posts_table 2.生产文件 <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePostsTable extends Migration { /** *

Laravel 创建指定表 migrate

解决方案:打开创建表的那个 migration 文件,在创建表的方法执行之前加一个判断条件 if (!Schema::hasTable('password_resets')) { Schema::create('password_resets', function (Blueprint $table) { $table->string('email')->index(); $table->string('token'); $table->timestamp('created_at'