laravelÊý¾Ý±íÔõôɾ³ý
laravelÊÇÒ»¸ö¹ãÊܽӴýµÄphp webÓ¦ÓóÌÐò¿ò¼Ü£¬ËüÌṩÁËÐí¶àÀû±ãµÄ¹¦Ð§£¬Ê¹µÃwebÓ¦ÓóÌÐò¿ª·¢±äµÃÔ½·¢ÈÝÒ׺͸ßЧ¡£ÆäÖÐÒ»¸öÖ÷ÒªµÄ¹¦Ð§ÊÇ´¦ÀíÊý¾Ý¿âÊý¾Ý£¬°üÀ¨Êý¾Ý±íµÄɾ³ý¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÌÖÂÛÔõÑùÔÚlaravelÖÐɾ³ýÊý¾Ý±í£¬È·±£Êý¾ÝµÄÍêÕûÐÔºÍÒ»ÖÂÐÔ¡£
LaravelÖеÄÊý¾Ý¿âǨáã
ÔÚLaravelÖУ¬ÎÒÃÇʹÓÃÊý¾Ý¿âǨáãÀ´½¨Éè¡¢Ð޸Ļòɾ³ýÊý¾Ý±í¡£Êý¾Ý¿âǨáãÊÇLaravelÌṩµÄÒ»ÖÖÀû±ãµÄ·½·¨£¬ËüÔÊÐíÄú±àдPHP´úÂ룬ÒÔ±ãÇáËɵؽ¨Éè¡¢Ð޸Ļòɾ³ýÊý¾Ý±í£¬¶ø²»±ØÊÖ¶¯±àдSQLÓï¾ä¡£
Òª½¨ÉèÒ»¸öLaravelǨá㣬ÎÒÃÇ¿ÉÒÔʹÓÃArtisanÏÂÁîÐй¤¾ß¡£ÔÚÏÂÁîÐÐÖУ¬ÊäÈëÒÔÏÂÏÂÁ
php artisan make:migration create_example_table
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽ¨ÉèÒ»¸öÃûΪ¡°create_example_table¡±µÄǨáã¡£Õ⽫ÔÚdatabase\migrationsĿ¼Öн¨ÉèÒ»¸öеÄǨáãÎļþ¡£ÎÒÃÇ¿ÉÒÔÔÚÎļþÖÐʹÓÃLaravelÌṩµÄSchemaÀàÀ´½ç˵Êý¾Ý±íµÄ½á¹¹ºÍ×ֶΣ¬ÈçÏÂËùʾ£º
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateExampleTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('example', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('example'); } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽ¨ÉèÁËÒ»¸öÃûΪ¡°example¡±µÄÊý¾Ý±í£¬ËüÓÐÒ»¸ö×ÔÔöµÄID×ֶΡ¢Ò»¸öÎÊÌâ×ֶΡ¢Ò»¸öÄÚÈÝ×ֶκÍʱ¼ä´Á×ֶΡ£×¢ÖØ£¬ÎÒÃÇÒ²½ç˵ÁËreverseÒªÁìdown()£¬ÒÔ±ãÔÚÐèҪʱ¿ÉÒÔ×÷·Ï¸ÃǨáã¡£
ɾ³ýÊý¾Ý±í
ÔÚLaravelÖÐɾ³ýÊý¾Ý±íºÜÊǼòÆÓ£¬ÎÒÃÇ¿ÉÒÔʹÓÃArtisanÏÂÁîÐй¤¾ßµÄmigrate:rollbackÏÂÁî¡£Õâ¸öÏÂÁ»Ø¹ö×îºóÒ»¸öǨáãÎļþ£¬Ò²¾ÍÊǸոս¨ÉèµÄ¡°create_example_table¡±Ç¨áã¡£
ÔÚÏÂÁîÐÐÖУ¬ÊäÈëÒÔÏÂÏÂÁ
php artisan migrate:rollback
µÇ¼ºó¸´ÖÆ
´ËÏÂÁ»Ø¹ö¡°create_example_table¡±Ç¨áãÎļþ£¬²¢É¾³ý¡°example¡±Êý¾Ý±í¼°ÆäËùÓÐÊý¾Ý¡£
×¢ÖØ£¬ÈôÊÇÄúÏëɾ³ý¸ü¶àµÄÊý¾Ý±í£¬Äú¿ÉÒԻعöµ½ÐèҪɾ³ýµÄ×îºóÒ»¸öǨáãÎļþ£¬È»ºó½«down()ÒªÁìÖеÄÏìÓ¦dropIfExists()º¯Êý×÷·Ï×¢ÊÍ¡£
ɾ³ýÊý¾Ý±íµ«±£´æÊý¾Ý
ÈôÊÇÄúÏë±£´æÊý¾Ý²¢É¾³ýÊý¾Ý±í£¬LaravelÌṩÁËÁíÒ»ÖÖ¹¦Ð§£¬¼´É¾³ýÊý¾Ý±íµ«±£´æÊý¾ÝµÄǨáã¡£ÎÒÃÇ¿ÉÒÔʹÓÃArtisanÏÂÁîÐй¤¾ßµÄmake:drop-pretendÏÂÁîÀ´½¨ÉèÕâÖÖÀàÐ͵ÄǨáã¡£
ÔÚÏÂÁîÐÐÖУ¬ÊäÈëÒÔÏÂÏÂÁ
php artisan make:drop-pretend example
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽ«½¨ÉèÒ»¸öÃûΪ¡°drop_example_table¡±µÄǨáãÎļþ¡£Õ⽫ÔÚdatabase\migrationsĿ¼Öн¨ÉèÒ»¸öеÄǨáãÎļþ¡£ÎÒÃÇ¿ÉÒÔÔÚÎļþÖÐʹÓÃLaravelÌṩµÄSchemaÀàÀ´½ç˵Ҫɾ³ýµÄÊý¾Ý±íºÍÄÚÈÝ£¬ÈçÏÂËùʾ£º
use Illuminate\Database\Migrations\Migration; use Illuminate\Support\Facades\Schema; class DropExampleTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('example', function ($table) { DB::statement('SET FOREIGN_KEY_CHECKS=0;'); $table->drop(); DB::statement('SET FOREIGN_KEY_CHECKS=1;'); }); } /** * Reverse the migrations. * * @return void */ public function down() { // This migration cannot be reversed. } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇʹÓÃÁËtable()ÒªÁ첢ת´ïÁËÊý¾Ý±íÃûÀ´½ç˵Ҫɾ³ýµÄÊý¾Ý±í¡£È»ºó£¬ÔÚup()ÒªÁìÖУ¬ÎÒÃÇʹÓÃÁËdrop()ÒªÁìÀ´É¾³ýÊý¾Ý±í¡£ÎªÁË×èÖ¹ÓÉÓÚÍâ¼üÔ¼Êø¶øÒý·¢µÄ¹ýʧ£¬ÎÒÃÇʹÓÃÁËDB::statement()º¯ÊýÀ´¹Ø±ÕºÍ·¿ªMySQLµÄÍâ¼üÔ¼Êø¼ì²é¡£ÔÚdown()ÒªÁìÖУ¬ÎÒÃÇûÓнç˵ÈκÎÄÚÈÝ£¬ÓÉÓÚÕâ¸öǨáãÊDz»¿É±»·´×ªµÄ¡£
×ܽá
ÔÚLaravelÖУ¬´¦ÀíÊý¾Ý¿âÊý¾ÝÊÇWebÓ¦ÓóÌÐò¿ª·¢µÄÖ÷Òª²¿·Ö¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËʹÓÃLaravelÊý¾Ý¿âǨáãÀ´É¾³ýÊý¾Ý±íµÄÒªÁì¡£ÎÒÃÇʹÓÃArtisanÏÂÁîÐй¤¾ßÀ´½¨ÉèºÍ»Ø¹öǨáãÎļþ£¬Í¬Ê±°ü¹ÜÁËÊý¾ÝµÄÒ»ÖÂÐÔºÍÍêÕûÐÔ¡£ÎÒÃÇ»¹ÏÈÈÝÁËÔõÑùʹÓÃɾ³ýÊý¾Ý±íµ«±£´æÊý¾ÝµÄǨáã¡£ÕâЩ¶¼ÊÇLaravelÖкÜÊÇÊÊÓõĹ¦Ð§£¬ÎÒÃÇ¿ÉÒÔÔÚLaravelÓ¦ÓóÌÐòµÄ¿ª·¢ÖÐʹÓÃËüÃÇÀ´´¦ÀíÊý¾Ý¿âÊý¾Ý£¬ÒԱ㿪·¢Ô½·¢¸ßЧºÍÀû±ã¡£
ÒÔÉϾÍÊÇlaravelÊý¾Ý±íÔõôɾ³ýµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡