laravelÊý¾Ý±íÔõôÉèÖÃ
laravelÊÇÏÖ´úphp¿ª·¢ÖÐ×îÊ¢ÐеĿò¼ÜÖ®Ò»¡£ËüÌṩÁËÐí¶àÓÐÓõĹ¦Ð§ºÍ¹¤¾ß£¬ÆäÖÐ×îÖ÷ÒªµÄÊÇËüµÄorm¡£¿ÉÒÔʹÓÃorm£¨¹¤¾ß¹ØϵӳÉ䣩ÇáËɵزÙ×÷Êý¾Ý¿â¡£ÔÚlaravelÖУ¬Ê¹ÓÃorm½¨ÉèÊý¾Ý±í²¢ÇÒÉèÖÃÊý¾Ý±íµÄÒªÁìºÜÊǼòÆÓ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÌÖÂÛÔõÑùʹÓÃlaravelÀ´ÉèÖÃÊý¾Ý±í¡£
LaravelÊý¾Ý¿âǨáã
ÔÚLaravelÖУ¬Êý¾Ý±íµÄÉèÖÃÊÇͨ¹ýÊý¾Ý¿âǨá㣨database migration£©À´Íê³ÉµÄ¡£Êý¾Ý¿âǨáãÊÇÒ»ÖÖ°æ±¾¿ØÖÆ£¬ÓÃÓÚÖÎÀíÊý¾Ý±íµÄ½á¹¹×ª±ä¡£Ã¿Ò»¸öǨá㶼ÐÎòÁËÊý¾Ý¿âģʽµÄת±ä¡£Ã¿¸ö°æ±¾¿ØÖÆǨá㶼·ºÆðÁËÒ»´ÎÊý¾Ý¿âÉý¼¶£¬ÒÔÊÇÄã¿ÉÒԻص½Ö®Ç°µÄ°æ±¾»òÕßÉý¼¶µ½½Ïеİ汾¡£¸üÖ÷ÒªµÄÊÇ£¬LaravelÌṩÁ˱ã½ÝµÄÏÂÁîÐй¦Ð§À´½¨ÉèºÍÖÎÀíǨáã¡£ÕâÑùÄã¾Í¿ÉÒÔºÜÈÝÒ×µØÏòÊý¾Ý¿âÌí¼Ó»òɾ³ý±í¸ñ¡£Òò´Ë£¬ÔÚÕâ¸öÀú³ÌÖУ¬Äã²»±ØÊÖ¶¯±àдSQL´úÂë¡£
LaravelǨáãµÄ½¨Éè
Ê×ÏÈ£¬ÔÚLaravelÖн¨ÉèǨáãºÜÊǼòÆÓ¡£¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´½¨ÉèÒ»¸öÐÂǨá㣺
php artisan make:migration create_users_table
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁ½¨ÉèÒ»¸öеÄǨá㣬ÃûΪ¡°create_users_table¡±¡£Ç¨á㽫ÉúÑÄÔÚ¡°database/migrations¡±Ä¿Â¼Ï¡£Õâ¸öĿ¼¾ÍÊÇLaravel¿ò¼Ü×Ô´øµÄÊý¾Ý¿âǨáãĿ¼¡£
·¿ª¸Õ½¨ÉèµÄǨáã¡£Ä㽫¿´µ½Ò»¸ö¿ÕµÄ¡°up¡±ÒªÁ죬ÕâÊǵ±ÄãǨáãʱִÐеĴúÂë¡£ÏÂÃæÊÇÕâ¸öÒªÁìµÄ¼òÆӵĽṹ£º
public function up() { // }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÒªÁìÖУ¬Äã¿ÉÒÔ±àдÓÃÓÚ½¨ÉèÊý¾Ý±íµÄ´úÂë¡£ÎÒÃǽ«ÔÚÏÂÃæµÄʾÀýÖÐÚ¹ÊÍ¡£
LaravelÊý¾ÝÀàÐÍ
ÔÚLaravelÖУ¬ÓжàÖÖÊý¾ÝÀàÐͿɹ©Ñ¡Ôñ£¬ÓÃÓÚÐÎòÊý¾Ý±íÁеÄÀàÐÍ¡£ÔÚÏÂÃæµÄʾÀýÖУ¬ÎÒÃǽ«ÏÈÈÝһЩ¿ÉÓõÄÊý¾ÝÀàÐÍ¡£
1.×ÔÔöID
$table->bigIncrements(‘id’)
2.ÕûÊý
$table->integer(‘age’)
3.³¤ÕûÊý
$table->bigInteger(‘views’)
4.СÊý
$table->decimal(‘price’, 5, 2)
5.×Ö·û´®
$table->string(‘name’)
6.³¤Îı¾
$table->longText(‘description’)
7.ÈÕÆÚ
$table->date(‘dob’)
8.ÈÕÆÚʱ¼ä
$table->dateTime(‘published_at’)
9.²¼¶û
$table->boolean(‘is_active’)
10.JSON
$table->json(‘options’)
11.ʱ¼ä´Á
$table->timestamps()
LaravelÊý¾Ý±íµÄÉèÖÃ
ʹÓÃÉÏÃæµÄÏÂÁÉèǨáã²¢·¿ª¡°up¡±ÒªÁì¡£½ÓÏÂÀ´£¬ÄãÐèÒªÉèÖÃÊý¾Ý±íµÄÃû³Æ¡¢ÁÐÒÔ¼°ËüÃǵÄÀàÐͺÍÆäËûϸ½Ú¡£ÏÂÃæÊÇÔõÑùÉèÖÃÒ»¸öÃûΪ¡°users¡±µÄÊý¾Ý±í£º
public function up() { Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇͨ¹ýŲÓá°Schema::create¡±ÒªÁìÀ´½¨ÉèÊý¾Ý±í¡£ÔÚŲÓá°Schema::create¡±ÒªÁìʱ£¬ÎÒÃÇÐèҪת´ïÊý¾Ý±íÃû³ÆºÍÒ»¸ö»Øµ÷ÒªÁ죬Õâ¸ö»Øµ÷ÒªÁìÓÃÓÚÐÎòÕâ¸öÊý¾Ý±íµÄÁкÍÆäËûϸ½Ú¡£Ôڻص÷ÒªÁìÖУ¬Ê¹Óá°Blueprint¡±ÀཨÉèÁС£È磬ÎÒÃǽ¨ÉèÁËÒ»¸öÃû³ÆΪ¡°id¡±µÄ×ÔÔöÁУ¬²¢ÇÒÕâ¸öÁÐÊÇ´ËÊý¾Ý±íµÄÖ÷¼ü¡£ÁеÄÃû³ÆʹÓÃ×Ö·û´®À´ÌåÏÖ£¬ÁеÄÀàÐÍ¿ÉÒÔʹÓÃÉÏÃæÁгöµÄÈκÎÊý¾ÝÀàÐÍ¡£
ŲÓá°Schema::create¡±ÒªÁ콫ÔÚÊý¾Ý¿âÖн¨ÉèÒ»¸öÃûΪ¡°users¡±µÄ±í¡£ÈôÊÇÊý¾Ý±íÒѾ±£´æ£¬ÄãÈÔÈ»¿ÉÒÔʹÓÃÕâ¸öÏÂÁîÀ´ÏòËüÌí¼Ó¸ü¶àµÄÁлò¸ü¸ÄËüµÄ½á¹¹¡£ÎªÁËÍê³ÉÕâ¸öʹÃü£¬ÄãÐèҪʹÓá°Schema::table¡±ÒªÁ죬²¢Ôڻص÷º¯ÊýÖÐÖ¸¶¨¸ü¸Ä¡£
ÀýÈ磬ÏÂÃæµÄ´úÂ뽫ÏòÃûΪ¡°users¡±µÄÊý¾Ý±íÌí¼ÓÒ»¸öÃûΪ¡°phone¡±µÄÁУº
public function up() { Schema::table('users', function (Blueprint $table) { $table->string('phone')->after('email'); }); }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇͨ¹ýŲÓá°Schema::table¡±ÒªÁìÀ´¸ü¸üÃûΪ¡°users¡±µÄÊý¾Ý±í¡£¸ü¸ÄµÄÏêÇéÊÇÈÃǨá㽨ÉèÒ»¸öÃûΪ¡°phone¡±µÄÁУ¬²¢ÇÒ½«Æä°²ÅÅÔÚ¡°email¡±ÁÐÖ®ºó¡£
LaravelǨáãµÄÔËÐÐ
ÏÖÔÚ£¬ÎÒÃÇÒѾ½¨ÉèÁËÒ»¸ö¼òÆÓµÄÊý¾Ý±í¡£½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÔËÐÐǨáã²¢½«¸ü¸ÄÉúÑĵ½Êý¾Ý¿âÖС£ÒªÔËÐÐǨá㣬ÇëʹÓÃÒÔÏÂÏÂÁ
php artisan migrate
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁÔËÐÐËùÓÐÉÐδÔËÐеÄǨáã¡£
LaravelǨáãµÄ»Ø¹ö
LaravelÌṩÁËÒ»¸öÀû±ãµÄÎÊÌâ»Ø¹öÏÂÁËüÓÃÓÚ³·»Ø֮ǰÔËÐеÄǨáã¡£ÔÚ³·»ØǨáãʱ£¬Laravel»á×Ô¶¯»Ö¸´¸ü¸ÄÇ°°æ±¾µÄÊý¾Ý±í½á¹¹¡£Òª»Ø¹öÒ»¸öǨá㣬ÇëʹÓÃÒÔÏÂÏÂÁ
php artisan migrate:rollback
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁ»Ø¹ö×îºóÒ»¸öǨáã¡£ÈôÊÇÄãÏë³·»Ø¸ü¶àµÄǨá㣬ÇëʹÓá°–step¡±²ÎÊýºÍÊý×ÖÀ´Ö¸¶¨Òª»Ø¹öµÄǨáãÊýÄ¿¡£ÀýÈ磺
php artisan migrate:rollback --step=2
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁ»Ø¹ö×îºóÁ½¸öǨáã¡£
×ܽá
LaravelÌṩÁËÒ»ÖÖÇáÓ¯ÒªÁìÀ´ÖÎÀíÊý¾Ý¿â½á¹¹×ª±ä¡£Ê¹ÓÃÊý¾Ý¿âǨá㣬¿ÉÒÔÇáËɵØÏòLaravelÓ¦ÓóÌÐòÌí¼Ó»òɾ³ýÊý¾Ý±í£¬ÕâÊÇΪ¼á³ÖÓÅÒìµÄ°æ±¾¿ØÖƶø±ØÐèµÄ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÌÖÂÛÁËÔÚLaravelÖÐÔõÑù½¨ÉèºÍÉèÖÃÊý¾Ý±í£¬ÒÔ¼°ÔõÑùÔËÐкͻعöǨáã¡£ÈôÊÇÄã×ñÕÕÕâЩ°ì·¨£¬Ä㽫Äܹ»ÔÚLaravelÖÐÇáËɵؽ¨ÉèºÍÖÎÀíÊý¾Ý±í¡£
ÒÔÉϾÍÊÇlaravelÊý¾Ý±íÔõôÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡