laravelÔõôÐÞ¸Ä×Ö¶ÎÀàÐÍ
½üÄêÀ´£¬laravelÒѾ³ÉΪÁËweb¿ª·¢ÁìÓòµÄÁìÍ·Ñò£¬²¢ÇÒÔ½À´Ô½¶àµÄÈËÔ¸ÒâʹÓÃËü×îÏÈ¿ª·¢×Ô¼ºµÄÏîÄ¿¡£ÓÉÓÚlaravel¾ßÓкܸߵÄÎÞаÐԺͿÉÀ©Õ¹ÐÔ¡£È»¶ø£¬ÔÚ¶ÔÊý¾Ý¿â¾ÙÐвÙ×÷µÄʱ¼ä£¬ÎÒÃǾ³£ÐèÒªÐ޸ıí¸ñµÄ½á¹¹£¬°üÀ¨×Ö¶ÎÀàÐÍ¡¢¾Þϸ¡¢Ä¬ÈÏÖµµÈµÈ¡£ÔÚÕâƪÎÄÕÂÖУ¬ÎÒÃǽ«ÌÖÂÛÔõÑùÔÚlaravel 5.4ÖÐÐÞ¸Ä×Ö¶ÎÀàÐÍ¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÏàʶLaravel Schema BuilderµÄ»ù´¡ÖªÊ¶¡£Schema BuilderÊÇLaravelµÄÒ»¸ö×é¼þ£¬ËüÌṩÁËÒ»ÖÖ¼òÆӵķ½·¨À´½¨ÉèºÍÐÞ¸ÄÊý¾Ý¿â±í¸ñµÄ½á¹¹¡£ÔÚÕâÀÎÒÃǽ«ÒÔMySQLΪÀý¾ÙÐÐÑÝʾ¡£
¼ÙÉèÎÒÃÇÓÐÒ»¸öÓû§±í¸ñ£¨users£©£¬ÆäÖÐÓÐÒ»¸öÃûΪ¡°age¡±µÄ×ֶΣ¬ËüµÄÊý¾ÝÀàÐÍΪÕûÊýÀàÐÍ£¨INT£©£¬ÏÖÔÚÎÒÃÇÐèÒª½«ÆäÐÞ¸ÄΪ×Ö·û´®ÀàÐÍ£¨VARCHAR£©¡£
°ì·¨1£º½¨ÉèǨáãÎļþ
LaravelµÄǨáãÎļþÊÇÊý¾Ý¿âÖбí¸ñ½á¹¹µÄ¡°À¶±¾¡±¡£ÎÒÃÇÐèÒª½¨ÉèÒ»¸öеÄǨáãÎļþÀ´Íê³É×Ö¶ÎÀàÐ͵ÄÐ޸ġ£
ʹÓÃArtisanÏÂÁîÐй¤¾ß£¬ÎÒÃÇ¿ÉÒÔÊäÈëÒÔÏÂÏÂÁ
php artisan make:migration modify_users_table --table=users
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁ»á½¨ÉèÒ»¸öÃûΪ¡°modify_users_table¡±µÄÐÂǨáãÎļþ¡£ÎÒÃÇÐèÒª·¿ª¸ÃÎļþ£¬²¢Ê¹ÓÃÏÂÃæµÄ´úÂëÔÚupÒªÁìÖбàдǨáãÂß¼£º
use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class ModifyUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('users', function (Blueprint $table) { $table->string('age')->change(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('users', function (Blueprint $table) { $table->integer('age')->change(); }); } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öǨáãÎļþÖУ¬ÎÒÃÇʹÓÃÁËSchema::tableÒªÁìÀ´²Ù×÷users±í¸ñ¡£È»ºóʹÓÃÁËBlueprint¹¤¾ßÀ´½¨Éè¶ÔеĽṹµÄ½ç˵¡£È»ºó£¬ÎÒÃÇʹÓÃchange()ÒªÁìÀ´Ð޸ġ°age¡±×ֶεÄÀàÐÍ¡£Õâ¸öÒªÌå»á¸æËßLaravel½«¡°age¡±×ֶεÄÊý¾ÝÀàÐÍÉèÖÃΪ×Ö·û´®ÀàÐÍ¡£
ÖµµÃ×¢ÖصÄÊÇ£¬ÈôÊÇÎÒÃÇÔÚͳһ¸öǨáãÎļþÖÐÌí¼ÓÁ˶à¸ö×ֶΣ¬Ôò¿ÉÒÔʹÓöà¸öchange()ÒªÁìÀ´Öð¸ö¸ü¸ÄËüÃǵÄÊý¾ÝÀàÐÍ¡£
°ì·¨2£ºÔËÐÐǨáãÎļþ
ÏÖÔÚÎÒÃÇÒѾ±àдºÃÁËǨáãÎļþ£¬¿ÉÒÔÔËÐÐÏÂÃæµÄÏÂÁîÈÃLaravelƾ֤ǨáãÎļþÀ´´¦Àíusers±í¸ñµÄ±ä»»¡£
php artisan migrate
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁ»áÔËÐÐËùÓÐÉÐδÔËÐеÄǨáãÎļþ£¬²¢½«Æä¼Í¼µ½Ç¨áãÎļþ±íÖС£ÒÔÊÇ£¬ÈôÊÇÄúµÄǨáãÎļþÊǵÚÒ»¸ö»òÕßΨÖðÒ»¸öÕýÔÚÔËÐеÄǨáãÎļþ£¬Ôò²»ÐèҪʹÓÖpretendÑ¡Ïî¡£
°ì·¨3£ºÐ£Ñé
ÔÚǨáãÀֳɺó£¬ÎÒÃÇ¿ÉÒÔÈ¥Êý¾Ý¿âÖÐÉó²éusers±í¸ñµÄ½á¹¹£¬ÒÔÈ·±£ÎÒÃÇÐ޸ĵÄÊý¾ÝÀàÐÍ×Ö¶ÎÒѾÉúЧ¡£ÔÚMySQLЧÀÍÆ÷ÉÏ£¬¿ÉÒÔʹÓÃÏÂÃæµÄÏÂÁ
DESCRIBE users;
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁ»áÏÔʾ³öusers±í¸ñµÄ½á¹¹£¬ÒÔ¼°ÎÒÃÇÔÚǨáãÎļþÖÐÐ޸ĵÄÊý¾ÝÀàÐ͵Ä×ֶΡ£ÈôÊÇÐÞ¸ÄÀֳɣ¬ÔòÓ¦¸Ã¿ÉÒÔ¿´µ½ËüµÄÀàÐÍÒѾ±»ÉèÖÃΪVARCHARÀàÐÍ¡£
+-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | id | int(10) | NO | PRI | NULL | auto_increment | | name | varchar(255) | NO | | NULL | | | age | varchar(255) | NO | | NULL | | +-------+--------------+------+-----+---------+----------------+
µÇ¼ºó¸´ÖÆ
×ܽá
ÔÚLaravelÖУ¬ÐÞ¸Ä×ֶεÄÊý¾ÝÀàÐÍÊÇÒ»¼þºÜÊǼòÆÓµÄÊÂÇé¡£ÎÒÃÇÖ»ÐèÒª±àдһ¸öеÄǨáãÎļþ£¬²¢Ê¹ÓÃSchema BuilderµÄAPI¼´¿ÉÇáËÉÍê³É¡£ÈôÊÇÄúÏëÒª¸ü¸ÄÆäËû·½ÃæµÄ±í¸ñ½á¹¹£¬Ò²¿ÉÒÔÇáËɵØͨ¹ýÐÞ¸ÄǨáãÎļþÀ´Íê³É¡£×ÜÖ®£¬LaravelÕæµÄÊÇWeb¿ª·¢ÖеÄÒ»¿îÇ¿Ê¢¹¤¾ß£¬ÎÞÂÛÊÇÔÚÐ޸ıí¸ñ½á¹¹ÕÕ¾ÉʵÏÖÆäËû¹¦Ð§·½Ã棬Ëü¶¼¿ÉÒÔÈÃÄúÔ½·¢ÇáËÉ×ÔÈôµØʵÏÖ¡£
ÒÔÉϾÍÊÇlaravelÔõôÐÞ¸Ä×Ö¶ÎÀàÐ͵ÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡