Laravel Migrate

artisan命令行建立migratephp

格式:laravel

php artisan make:migration YourFileName

示例:bash

php artisan make:migration create_books_table

咱們找到laravel目錄下database\migrations\2017_XX_XX_XXXXXX_create_books_table.phpapp

注意:XX表明時間戳,因時而異函數

<?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類post

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函數改寫成ui

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/this

一切準備就緒,咱們開始遷移spa

php artisan migrate

若是你遷移後發覺並非你想要的數據表,能夠回滾命令行

php artisan migrate:rollback

這裏要給你們提個醒,down方法必定要寫,可能有些人這個方法會空着,以爲只要數據表建立出來了就沒事了,可是一旦涉及rollback又沒有down方法,你的migrate操做就是不可逆的!  

就像這樣:

<?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使用了門面模式

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();
    }
}
相關文章
相關標籤/搜索