Migration là gì?
Tạo dữ liệu với Migration
cd C:\xampp\htdocs\myproject\
php artisan make:migration create_news_table
Dòng lệnh trên thực thi sẽ cho ta kết quả sau:
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateNewsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('news', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('news'); } }
Với nội dung trên, ta đã có thể tạo bảng news với 1 cột là id, để có thể nhiều cột hơn ta thêm trực tiếp vào bên trong function up():news với 1 cột là id, để có thể nhiều cột hơn ta thêm trực tiếp vào bên trong function up(): <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateNewsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('news', function (Blueprint $table) { $table->increments('id'); $table->string('headline'); $table->string('email'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('news'); } }
Xử lý lỗi khi chạy lệnh Migration
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Schema; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { Schema::defaultStringLength(191); } /** * Register any application services. * * @return void */ public function register() { // } }
|