2017年9月10日 星期日

Laravel,migration,建立表格(自訂)


/*cmd指令*/
php artisan make:migration create_posts_table --create=posts


<?php
/*檔案:database/migrations/2017_09_10_152910_create_posts_table.php*/

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

class CreatePostsTable extends Migration
{

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

public function down()
{
Schema::dropIfExists('posts');
}
}


 



 



    /*檔案:database/migrations/2017_09_10_152910_create_posts_table.php*/

public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
// ->nullable() //->default() //->unique()
// 可以空 預設值 不可重複
$table->text('content');
$table->timestamps();
});
}


/*cmd指令*/
php artisan migrate


/*建立表格SQL*/

CREATE TABLE `posts` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`content` text COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE `posts`
ADD PRIMARY KEY (`id`);

ALTER TABLE `posts`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;COMMIT;


 



 



 



/*cmd指令*/
php artisan migrate:rollback


如此將會執行上一個migration裡面的down函式 (預設是刪掉表格)



沒有留言:

張貼留言