ÔõÑùʹÓÃLaravelÐÞ¸ÄÊý¾Ý¿â
laravelÊÇÒ»¿îÊ¢ÐеÄphp¿ò¼Ü£¬Í¨¹ý³ä·ÖʹÓÃÏÖ´ú»¯µÄphpÓïÑÔÌØÕ÷À´¹¹½¨¸ßЧÇÒÒ×ÓÚά»¤µÄwebÓ¦ÓóÌÐò¡£ÆäÖÐʹÓÃ×î¶àµÄ¹¦Ð§Ö®Ò»¾ÍÊÇÓëÊý¾Ý¿â½»»¥£¬ÊµÏÖÊý¾ÝµÄÔöɾ¸Ä²é¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃlaravelÐÞ¸ÄÊý¾Ý¿â¡£
ÉèÖÃÊý¾Ý¿âÅþÁ¬
ÔÚʹÓÃLaravel²Ù×÷Êý¾Ý¿â֮ǰ£¬ÎÒÃÇÐèÒªÏÈÉèÖÃÊý¾Ý¿âÅþÁ¬¡£·¿ª.envÎļþ£¬ÐÞ¸ÄÆäÖеÄÒÔÏÂÉèÖÃÏ
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_database_name DB_USERNAME=your_database_username DB_PASSWORD=your_database_password
µÇ¼ºó¸´ÖÆ
½«your_database_name¡¢your_database_usernameºÍyour_database_password¸ÄΪÄãµÄMySQLÊý¾Ý¿âµÄÃû³Æ¡¢Óû§ÃûºÍÃÜÂë¡£
½¨ÉèÊý¾Ý¿âǨáãÎļþ
ÔÚLaravelÖУ¬ÎÒÃÇʹÓÃǨáãÎļþÀ´½¨ÉèºÍÐÞ¸ÄÊý¾Ý¿â±í½á¹¹¡£¿ÉÒÔͨ¹ýÏÂÁîÐй¤¾ßphp artisanÀ´½¨Éèusers±íµÄǨáãÎļþ¡£ÔÚÖÕ¶ËÖÐÊäÈëÒÔÏÂÏÂÁ
php artisan make:migration modify_users_table --table=users
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁÔÚdatabase/migrationsĿ¼Ï½¨ÉèÒ»¸öǨáãÎļþ£¬²¢ÇÒÔÚÃüÃû¹æÔòÖаüÀ¨ÁËmodify_users_tableÕâ¸ö´ÊÓï¡£ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔ±à¼Õâ¸öÎļþÀ´ÐÞ¸ÄÊý¾Ý¿â±í½á¹¹¡£
ÐÞ¸ÄÊý¾Ý¿â±í½á¹¹
ÔÚǨáãÎļþÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃupÒªÁìÀ´ÐÞ¸ÄÊý¾Ý¿â±í½á¹¹¡£ÏÂÃæÊÇÒ»¸öÐÞ¸Äusers±íÔöÌíphone×ֶεÄʾÀý£º
public function up() { Schema::table('users', function (Blueprint $table) { $table->string('phone')->after('name')->nullable(); }); }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃÇÔÚusers±íÖÐÔöÌíÁËÒ»¸öphone×ֶΣ¬²¢ÉèÖÃÆäÔÚname×Ö¶ÎÖ®ºó¡£nullable()ÒªÁìÌåÏÖ¸Ã×Ö¶ÎÔÊÐíΪ¿Õ¡£ÔÚ¶Ô±í¸ñÄÚÈݾÙÐÐÔöɾ¸Ä²éʱ£¬ÎªÁËÀû±ãÖÎÀí£¬ÎÒÃÇ»¹¿ÉÒÔÔÚ¶ÔÓ¦µÄdownÒªÁìÖмõÈ¥Õâ¸ö×ֶΣº
public function down() { Schema::table('users', function (Blueprint $table) { $table->dropColumn('phone'); }); }
µÇ¼ºó¸´ÖÆ
ÔËÐÐÊý¾Ý¿âǨáã
Íê³ÉÉÏÊöÐÞ¸ÄÖ®ºó£¬ÎÒÃÇÐèÒª½«Ç¨áãÎļþÓ¦Óõ½Êý¾Ý¿âÖС£ÔÚÖÕ¶ËÖÐÊäÈëÒÔÏÂÏÂÁ
php artisan migrate
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁִÐÐËùÓÐδӦÓõ½Êý¾Ý¿âµÄǨáãÎļþµÄupÒªÁ죬²¢½«ÆäÓ¦Óõ½Êý¾Ý¿âÖС£ÈôÊÇÔÚÓ¦ÓÃǨáãÎļþʱÓöµ½¹ýʧ£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî»Ø¹öǨá㣺
php artisan migrate:rollback
µÇ¼ºó¸´ÖÆ
»Ø¹öÏÂÁִÐÐËùÓÐÒѾӦÓõ½Êý¾Ý¿âµÄǨáãÎļþµÄdownÒªÁ죬²¢½«Æä´ÓÊý¾Ý¿âÖÐɾ³ý¡£
ʹÓÃLaravelÐÞ¸ÄÊý¾Ý¿â¼Í¼
Íê³ÉÁËÉÏÊöÐÞ¸ÄÖ®ºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃLaravelµÄEloquent ORMÀ´ÐÞ¸ÄÊý¾Ý¿â¼Í¼¡£ÏÂÃæÊÇÒ»¸öʹÓÃEloquentÐÞ¸Äusers±íÖмͼµÄʾÀý£º
$user = User::find(1); $user->phone = '1234567890'; $user->save();
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃÇʹÓÃfindÒªÁìÕÒµ½idΪ1µÄÓû§¼Í¼£¬²¢½«ÆäµÄphone×Ö¶ÎÖÃΪ1234567890£¬È»ºóʹÓÃsaveÒªÁìÉúÑÄÐ޸ġ£
×ܽá
LaravelÌṩÁË×Ô¶¯»¯µÄǨá㹤¾ßÀ´Í¬²½Êý¾Ý¿â±í½á¹¹ºÍ´úÂë¡£ÐèҪעÖصÄÊÇ£¬ÔÚÐ޸ıí½á¹¹Ö®ºó£¬±ØÐèÔËÐÐǨáãÎļþ½«Êý¾Ý¿â¸üÐÂΪ×îÐÂ״̬£¬Ï´ÎÐÞ¸ÄÐèÒªÒ²ÐèÒªÔËÐÐǨáãÎļþ¡£Ê¹ÓÃEloquent ORM¿ÉÒÔÀû±ãµØ²Ù×÷Êý¾Ý¿â£¬ÊµÏÖÊý¾ÝµÄÔöɾ¸Ä²é¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃLaravelÐÞ¸ÄÊý¾Ý¿âµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡