Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel MigrationÖÎÀíÊý¾Ý¿âǨá㣿
ÔÚlaravel¿ª·¢ÖУ¬Êý¾Ý¿âµÄÖÎÀíÊǺÜÊÇÖ÷ÒªµÄÒ»²¿·Ö¡£¶ølaravel migrationÌṩÁËÒ»ÖÖ±ã½ÝµÄ·½·¨À´ÖÎÀíÊý¾Ý¿âǨáã¡£½ÓÏÂÀ´ÈÃÎÒÃÇÒ»ÆðÀ´Ñ§Ï°ÔõÑùʹÓÃlaravel migrationÖÎÀíÊý¾Ý¿âǨáã¡£
ʲôÊÇLaravel Migration?
Laravel MigrationÊÇÓÃÀ´ÖÎÀíÊý¾Ý¿âǨáãµÄ¹¤¾ß£¬¿ÉÒÔÓÃÀ´¼Í¼ËùÓйØÓÚÊý¾Ý¿âµÄÐ޸IJÙ×÷£¬°üÀ¨½¨Éè¡¢Ð޸ĺÍɾ³ý±í½á¹¹¡¢Ìí¼Ó¡¢Ð޸ĺÍɾ³ý×ֶΡ¢ÉèÖÃË÷ÒýºÍÍâ¼üÔ¼ÊøµÈµÈ¡£Í¨¹ýLaravel Migration£¬ÎÒÃÇÄܹ»ÇáËɵضÔÊý¾Ý¿â¾ÙÐÐÉý¼¶ºÍά»¤£¬¶ø²»»áµ¼ÖÂÊý¾Ýɥʧ»ò½á¹¹ÔÓÂÒ¡£
Laravel MigrationÔõÑùʹÓã¿
½¨ÉèLaravel Migration
Ê×ÏÈ£¬ÎÒÃÇÐèҪͨ¹ýÖÕ¶ËÏÂÁTerminal£©½¨ÉèÒ»¸öеÄLaravel Migration¡£ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁÉèеÄMigration£º
php artisan make:migration create_user_table
µÇ¼ºó¸´ÖÆ
ÔËÐÐÏÂÁîºó£¬Laravel Migration»áÔÚdatabase/migrationsĿ¼ÏÂ×Ô¶¯½¨ÉèÒ»¸öMigrationÎļþ£¬ÈçÏÂËùʾ£º
<?php use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema; class CreateUserTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('user', 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(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('user'); } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öMigrationÎļþÖУ¬ÎÒÃǽç˵ÁËÒ»¸ö½Ð×ö¡°create_user_table¡±µÄMigration£¬Ëü»á½¨ÉèÒ»¸ö¡°user¡±±í£¬¸Ã±íÓµÓÐid¡¢name¡¢email¡¢email_verified_at¡¢password¡¢remember_tokenºÍtimestamps×ֶΡ£
×¢ÖØ£ºLaravel Migration¹ØÓÚÊý¾Ý±íÃû±ØÐèҪΪ¸´ÊýÐÎʽ¡£
ÔËÐÐLaravel Migration
Íê³ÉMigrationÎļþµÄ±àд£¬ÏÂÒ»²½ÎÒÃÇÐèÒªÔËÐиÃMigrationÎļþ¡£ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÔËÐÐMigration£º
php artisan migrate
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÔËÐÐÏÂÁîºó£¬Laravel Migration»á×Ô¶¯Ö´ÐÐMigrationÎļþÖнç˵µÄËùÓвÙ×÷²¢ÔÚÊý¾Ý¿âÖн¨ÉèÒ»¸öеġ°user¡±±í¡£
ÔÚÕâ¸öÀú³ÌÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÏÂÁîÐÐÊÓ²ìLaravel MigrationµÄÖ´ÐÐÇéÐΣ¬ÏàʶLaravel MigrationµÄ״̬¡£ÀýÈ磬ÈôÊÇÎÒÃÇÐèÒª¼ì²éÊý¾Ý¿âÖеÄMigration״̬£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
php artisan migrate:status
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁî»á·µ»ØËùÓÐMigrationÎļþµÄ״̬£¬°üÀ¨ÒѾִÐкʹýÖ´ÐеÄMigrationÎļþ¡£
ÐÞ¸ÄLaravel Migration
ÈôÊÇÐèÒª¶ÔÊý¾Ý¿âÖеıí½á¹¹¾ÙÐÐÐ޸Ļò¸üУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÐÞ¸ÄÒѱ£´æµÄMigrationÎļþʵÏÖ¡£ÀýÈ磬ÈôÊÇÎÒÃÇÐèÒª¸ø¡°user¡±±íÌí¼ÓÒ»¸ö¡°age¡±×ֶΣ¬¿ÉÒÔͨ¹ýÒÔÏÂÓγÌʵÏÖ£º
ÐÞ¸ÄMigrationÎļþ£¬Ìí¼Ó¡°age¡±×ֶΣº
$table->integer('age');
µÇ¼ºó¸´ÖÆ
ÔËÐÐLaravel Migration£º
php artisan migrate
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
µ±Laravel Migration¼ì²âµ½ÓÐеÄMigrationÎļþʱ£¬»á×Ô¶¯Ö´ÐÐËùÓÐеÄMigration²Ù×÷¡£
»Ø¹öLaravel Migration
Laravel MigrationÌṩÁËÒ»¸ö»Ø¹ö£¨rollback£©²Ù×÷£¬¿ÉÒÔÖ´ÐÐËùÓÐÒÔÇ°Ö´ÐйýµÄMigrationÎļþ£¬Ê¹Êý¾Ý¿â»Ø¹öµ½Ö®Ç°µÄ״̬¡£ÀýÈ磬ÈôÊÇÎÒÃÇÐèÒª»Ø¹ö֮ǰµÄMigrationÎļþ£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîʵÏÖ£º
php artisan migrate:rollback
µÇ¼ºó¸´ÖÆ
ÔËÐÐÏÂÁîºó£¬Laravel Migration»á×÷·ÏËùÓÐ×îеÄMigrationÎļþ£¬°ÑÊý¾Ý¿â»Ö¸´µ½×îеÄÖ´ÐÐǰ״̬¡£
×ܽá
Laravel MigrationÊÇÒ»¸öºÜÊÇÓÐÓõŤ¾ß£¬¿ÉÒÔÀû±ãÎÒÃÇÖÎÀíÊý¾Ý¿âǨá㡣ͨ¹ýLaravel Migration£¬ÎÒÃÇ¿ÉÒÔÇáËɵضÔÊý¾Ý¿â¾ÙÐÐÉý¼¶ºÍά»¤£¬²»ÐèÒªÊÖ¶¯¸ü¸ÄÊý¾Ý¿â£¬Ò²²»»áµ¼ÖÂÊý¾Ýɥʧ»ò½á¹¹ÔÓÂÒ¡£Òò´Ë£¬ÔÚLaravel¿ª·¢ÖкÏÀíÔËÓÃLaravel MigrationÊǺÜÊÇÐëÒªµÄ¡£
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel MigrationÖÎÀíÊý¾Ý¿âǨá㣿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡