{"id":22540,"date":"2024-11-21T11:22:28","date_gmt":"2024-11-21T03:22:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22540\/"},"modified":"2024-11-21T11:22:28","modified_gmt":"2024-11-21T03:22:28","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8laravel%e4%bf%ae%e6%94%b9%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22540\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Laravel\u4fee\u6539\u6570\u636e\u5e93"},"content":{"rendered":"<p>laravel\u662f\u4e00\u6b3e\u6d41\u884c\u7684php\u6846\u67b6\uff0c\u901a\u8fc7\u5145\u5206\u5229\u7528\u73b0\u4ee3\u5316\u7684php\u8bed\u8a00\u7279\u6027\u6765\u6784\u5efa\u9ad8\u6548\u4e14\u6613\u4e8e\u7ef4\u62a4\u7684web\u5e94\u7528\u7a0b\u5e8f\u3002\u5176\u4e2d\u4f7f\u7528\u6700\u591a\u7684\u529f\u80fd\u4e4b\u4e00\u5c31\u662f\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u589e\u5220\u6539\u67e5\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528laravel\u4fee\u6539\u6570\u636e\u5e93\u3002<\/p>\n<ol>\n<li>\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5<\/li>\n<\/ol>\n<p>\u5728\u4f7f\u7528Laravel\u64cd\u4f5c\u6570\u636e\u5e93\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u3002\u6253\u5f00.env\u6587\u4ef6\uff0c\u4fee\u6539\u5176\u4e2d\u7684\u4ee5\u4e0b\u914d\u7f6e\u9879\uff1a<\/p>\n<pre>DB_CONNECTION=mysql\nDB_HOST=127.0.0.1\nDB_PORT=3306\nDB_DATABASE=your_database_name\nDB_USERNAME=your_database_username\nDB_PASSWORD=your_database_password<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5c06your_database_name\u3001your_database_username\u548cyour_database_password\u6539\u4e3a\u4f60\u7684MySQL\u6570\u636e\u5e93\u7684\u540d\u79f0\u3001\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u6570\u636e\u5e93\u8fc1\u79fb\u6587\u4ef6<\/li>\n<\/ol>\n<p>\u5728Laravel\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u8fc1\u79fb\u6587\u4ef6\u6765\u521b\u5efa\u548c\u4fee\u6539\u6570\u636e\u5e93\u8868\u7ed3\u6784\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u5de5\u5177php artisan\u6765\u521b\u5efausers\u8868\u7684\u8fc1\u79fb\u6587\u4ef6\u3002\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>php artisan make:migration modify_users_table --table=users<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u547d\u4ee4\u5c06\u5728database\/migrations\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u8fc1\u79fb\u6587\u4ef6\uff0c\u5e76\u4e14\u5728\u547d\u540d\u89c4\u5219\u4e2d\u5305\u542b\u4e86modify_users_table\u8fd9\u4e2a\u8bcd\u8bed\u3002\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u8f91\u8fd9\u4e2a\u6587\u4ef6\u6765\u4fee\u6539\u6570\u636e\u5e93\u8868\u7ed3\u6784\u3002<\/p>\n<ol>\n<li>\u4fee\u6539\u6570\u636e\u5e93\u8868\u7ed3\u6784<\/li>\n<\/ol>\n<p>\u5728\u8fc1\u79fb\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528up\u65b9\u6cd5\u6765\u4fee\u6539\u6570\u636e\u5e93\u8868\u7ed3\u6784\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4fee\u6539users\u8868\u589e\u52a0phone\u5b57\u6bb5\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>public function up()\n{\n    Schema::table('users', function (Blueprint $table) {\n        $table-&gt;string('phone')-&gt;after('name')-&gt;nullable();\n    });\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728users\u8868\u4e2d\u589e\u52a0\u4e86\u4e00\u4e2aphone\u5b57\u6bb5\uff0c\u5e76\u8bbe\u7f6e\u5176\u5728name\u5b57\u6bb5\u4e4b\u540e\u3002nullable()\u65b9\u6cd5\u8868\u793a\u8be5\u5b57\u6bb5\u5141\u8bb8\u4e3a\u7a7a\u3002\u5728\u5bf9\u8868\u683c\u5185\u5bb9\u8fdb\u884c\u589e\u5220\u6539\u67e5\u65f6\uff0c\u4e3a\u4e86\u65b9\u4fbf\u7ba1\u7406\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u5bf9\u5e94\u7684down\u65b9\u6cd5\u4e2d\u51cf\u53bb\u8fd9\u4e2a\u5b57\u6bb5\uff1a<\/p>\n<pre>public function down()\n{\n    Schema::table('users', function (Blueprint $table) {\n        $table-&gt;dropColumn('phone');\n    });\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u8fd0\u884c\u6570\u636e\u5e93\u8fc1\u79fb<\/li>\n<\/ol>\n<p>\u5b8c\u6210\u4e0a\u8ff0\u4fee\u6539\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8fc1\u79fb\u6587\u4ef6\u5e94\u7528\u5230\u6570\u636e\u5e93\u4e2d\u3002\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>php artisan migrate<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u547d\u4ee4\u5c06\u6267\u884c\u6240\u6709\u672a\u5e94\u7528\u5230\u6570\u636e\u5e93\u7684\u8fc1\u79fb\u6587\u4ef6\u7684up\u65b9\u6cd5\uff0c\u5e76\u5c06\u5176\u5e94\u7528\u5230\u6570\u636e\u5e93\u4e2d\u3002\u5982\u679c\u5728\u5e94\u7528\u8fc1\u79fb\u6587\u4ef6\u65f6\u9047\u5230\u9519\u8bef\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u56de\u6eda\u8fc1\u79fb\uff1a<\/p>\n<pre>php artisan migrate:rollback<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u56de\u6eda\u547d\u4ee4\u5c06\u6267\u884c\u6240\u6709\u5df2\u7ecf\u5e94\u7528\u5230\u6570\u636e\u5e93\u7684\u8fc1\u79fb\u6587\u4ef6\u7684down\u65b9\u6cd5\uff0c\u5e76\u5c06\u5176\u4ece\u6570\u636e\u5e93\u4e2d\u5220\u9664\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528Laravel\u4fee\u6539\u6570\u636e\u5e93\u8bb0\u5f55<\/li>\n<\/ol>\n<p>\u5b8c\u6210\u4e86\u4e0a\u8ff0\u4fee\u6539\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Laravel\u7684Eloquent ORM\u6765\u4fee\u6539\u6570\u636e\u5e93\u8bb0\u5f55\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Eloquent\u4fee\u6539users\u8868\u4e2d\u8bb0\u5f55\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>$user = User::find(1);\n$user-&gt;phone = '1234567890';\n$user-&gt;save();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528find\u65b9\u6cd5\u627e\u5230id\u4e3a1\u7684\u7528\u6237\u8bb0\u5f55\uff0c\u5e76\u5c06\u5176\u7684phone\u5b57\u6bb5\u7f6e\u4e3a1234567890\uff0c\u7136\u540e\u4f7f\u7528save\u65b9\u6cd5\u4fdd\u5b58\u4fee\u6539\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>Laravel\u63d0\u4f9b\u4e86\u81ea\u52a8\u5316\u7684\u8fc1\u79fb\u5de5\u5177\u6765\u540c\u6b65\u6570\u636e\u5e93\u8868\u7ed3\u6784\u548c\u4ee3\u7801\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u4fee\u6539\u8868\u7ed3\u6784\u4e4b\u540e\uff0c\u5fc5\u987b\u8fd0\u884c\u8fc1\u79fb\u6587\u4ef6\u5c06\u6570\u636e\u5e93\u66f4\u65b0\u4e3a\u6700\u65b0\u72b6\u6001\uff0c\u4e0b\u6b21\u4fee\u6539\u9700\u8981\u4e5f\u9700\u8981\u8fd0\u884c\u8fc1\u79fb\u6587\u4ef6\u3002\u4f7f\u7528Eloquent ORM\u53ef\u4ee5\u65b9\u4fbf\u5730\u64cd\u4f5c\u6570\u636e\u5e93\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u589e\u5220\u6539\u67e5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Laravel\u4fee\u6539\u6570\u636e\u5e93\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>laravel\u662f\u4e00\u6b3e\u6d41\u884c\u7684php\u6846\u67b6\uff0c\u901a\u8fc7\u5145\u5206\u5229\u7528\u73b0\u4ee3\u5316\u7684php\u8bed\u8a00\u7279\u6027\u6765\u6784\u5efa\u9ad8\u6548\u4e14\u6613\u4e8e\u7ef4\u62a4\u7684web\u5e94\u7528\u7a0b\u5e8f\u3002\u5176\u4e2d\u4f7f\u7528\u6700\u591a\u7684\u529f\u80fd\u4e4b\u4e00\u5c31\u662f\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u589e\u5220\u6539\u67e5\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528laravel\u4fee\u6539\u6570\u636e\u5e93\u3002 \u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5 \u5728\u4f7f\u7528Laravel\u64cd\u4f5c\u6570\u636e\u5e93\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u3002\u6253\u5f00.env\u6587\u4ef6\uff0c\u4fee\u6539\u5176\u4e2d\u7684\u4ee5\u4e0b\u914d\u7f6e\u9879\uff1a 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 \u767b\u5f55\u540e\u590d\u5236 \u5c06your_database_name\u3001your_database_username\u548cyour_database_password\u6539\u4e3a\u4f60\u7684MySQL\u6570\u636e\u5e93\u7684\u540d\u79f0\u3001\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002 \u521b\u5efa\u6570\u636e\u5e93\u8fc1\u79fb\u6587\u4ef6 \u5728Laravel\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u8fc1\u79fb\u6587\u4ef6\u6765\u521b\u5efa\u548c\u4fee\u6539\u6570\u636e\u5e93\u8868\u7ed3\u6784\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u5de5\u5177php artisan\u6765\u521b\u5efausers\u8868\u7684\u8fc1\u79fb\u6587\u4ef6\u3002\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a php artisan make:migration modify_users_table &#8211;table=users \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4e2a\u547d\u4ee4\u5c06\u5728database\/migrations\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u8fc1\u79fb\u6587\u4ef6\uff0c\u5e76\u4e14\u5728\u547d\u540d\u89c4\u5219\u4e2d\u5305\u542b\u4e86modify_users_table\u8fd9\u4e2a\u8bcd\u8bed\u3002\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u8f91\u8fd9\u4e2a\u6587\u4ef6\u6765\u4fee\u6539\u6570\u636e\u5e93\u8868\u7ed3\u6784\u3002 \u4fee\u6539\u6570\u636e\u5e93\u8868\u7ed3\u6784 \u5728\u8fc1\u79fb\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528up\u65b9\u6cd5\u6765\u4fee\u6539\u6570\u636e\u5e93\u8868\u7ed3\u6784\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4fee\u6539users\u8868\u589e\u52a0phone\u5b57\u6bb5\u7684\u793a\u4f8b\uff1a public function up() { Schema::table(&#8216;users&#8217;, function (Blueprint $table) { $table-&gt;string(&#8216;phone&#8217;)-&gt;after(&#8216;name&#8217;)-&gt;nullable(); }); } \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728users\u8868\u4e2d\u589e\u52a0\u4e86\u4e00\u4e2aphone\u5b57\u6bb5\uff0c\u5e76\u8bbe\u7f6e\u5176\u5728name\u5b57\u6bb5\u4e4b\u540e\u3002nullable()\u65b9\u6cd5\u8868\u793a\u8be5\u5b57\u6bb5\u5141\u8bb8\u4e3a\u7a7a\u3002\u5728\u5bf9\u8868\u683c\u5185\u5bb9\u8fdb\u884c\u589e\u5220\u6539\u67e5\u65f6\uff0c\u4e3a\u4e86\u65b9\u4fbf\u7ba1\u7406\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u5bf9\u5e94\u7684down\u65b9\u6cd5\u4e2d\u51cf\u53bb\u8fd9\u4e2a\u5b57\u6bb5\uff1a public function down() { Schema::table(&#8216;users&#8217;, function (Blueprint $table) { $table-&gt;dropColumn(&#8216;phone&#8217;); }); } \u767b\u5f55\u540e\u590d\u5236 \u8fd0\u884c\u6570\u636e\u5e93\u8fc1\u79fb \u5b8c\u6210\u4e0a\u8ff0\u4fee\u6539\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8fc1\u79fb\u6587\u4ef6\u5e94\u7528\u5230\u6570\u636e\u5e93\u4e2d\u3002\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a php artisan migrate [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-22540","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=22540"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22540\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}