{"id":24799,"date":"2024-11-21T10:35:01","date_gmt":"2024-11-21T02:35:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24799\/"},"modified":"2024-11-21T10:35:01","modified_gmt":"2024-11-21T02:35:01","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8laravel%e4%b8%ad%e4%bd%bf%e7%94%a8%e4%b8%ad%e9%97%b4%e4%bb%b6%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e8%bf%81%e7%a7%bb","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24799\/","title":{"rendered":"\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u6570\u636e\u8fc1\u79fb"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169888844462159.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u6570\u636e\u8fc1\u79fb\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u6570\u636e\u8fc1\u79fb\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u6570\u636e\u8fc1\u79fb<\/p>\n<p>\u7b80\u4ecb<br \/>\u5728Laravel\u4e2d\uff0c\u6570\u636e\u8fc1\u79fb\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u7528\u4e8e\u7ba1\u7406\u6570\u636e\u5e93\u8868\u7ed3\u6784\u548c\u6570\u636e\u7684\u53d8\u5316\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f1a\u901a\u8fc7\u8fc1\u79fb\u6587\u4ef6\u6765\u521b\u5efa\u3001\u4fee\u6539\u548c\u5220\u9664\u6570\u636e\u5e93\u7684\u8868\u548c\u5b57\u6bb5\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5728\u6570\u636e\u8fc1\u79fb\u671f\u95f4\u6267\u884c\u4e00\u4e9b\u989d\u5916\u7684\u64cd\u4f5c\u3002\u8fd9\u65f6\uff0c\u4e2d\u95f4\u4ef6\u5c31\u53ef\u4ee5\u6d3e\u4e0a\u7528\u573a\u4e86\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5728Laravel\u4e2d\u5982\u4f55\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u6570\u636e\u8fc1\u79fb\uff0c\u5e76\u63d0\u4f9b\u8be6\u7ec6\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u6b65\u9aa4\u4e00\uff1a\u521b\u5efa\u8fc1\u79fb\u6587\u4ef6<br \/>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u8fc1\u79fb\u6587\u4ef6\uff0c\u7528\u4e8e\u5b9a\u4e49\u9700\u8981\u8fdb\u884c\u6570\u636e\u8fc1\u79fb\u7684\u6570\u636e\u5e93\u8868\u548c\u5b57\u6bb5\u3002\u901a\u8fc7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u5728Laravel\u9879\u76ee\u7684\u7ec8\u7aef\u4e2d\u521b\u5efa\u4e00\u4e2a\u8fc1\u79fb\u6587\u4ef6\uff1a<\/p>\n<pre>php artisan make:migration create_users_table<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u5728 database\/migrations \u6587\u4ef6\u5939\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a create_users_table.php \u7684\u8fc1\u79fb\u6587\u4ef6\u3002\u6253\u5f00\u8be5\u6587\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;?php use IlluminateDatabaseMigrationsMigration;\nuse IlluminateDatabaseSchemaBlueprint;\nuse IlluminateSupportFacadesSchema;\n\nclass CreateUsersTable extends Migration\n{\n    public function up()\n    {\n        Schema::create('users', function (Blueprint $table) {\n            $table-&gt;id();\n            $table-&amp;gt;string('name');\n            $table-&amp;gt;string('email')-&amp;gt;unique();\n            $table-&amp;gt;timestamps();\n        });\n    }\n\n    public function down()\n    {\n        Schema::dropIfExists('users');\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728 up \u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 Schema \u7c7b\u6765\u521b\u5efa users \u8868\uff0c\u5b9a\u4e49\u4e86 id\u3001name\u3001email \u548c timestamps \u5b57\u6bb5\u3002\u5728 down \u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 Schema \u7c7b\u5220\u9664 users \u8868\u3002<\/p>\n<p>\u6b65\u9aa4\u4e8c\uff1a\u521b\u5efa\u4e2d\u95f4\u4ef6<br \/>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u7c7b\uff0c\u7528\u4e8e\u5728\u6570\u636e\u8fc1\u79fb\u671f\u95f4\u6267\u884c\u989d\u5916\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u5728Laravel\u9879\u76ee\u7684\u7ec8\u7aef\u4e2d\u521b\u5efa\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u6587\u4ef6\uff1a<\/p>\n<pre>php artisan make:middleware MigrateMiddleware<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u5728 app\/Http\/Middleware \u6587\u4ef6\u5939\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a MigrateMiddleware.php \u7684\u4e2d\u95f4\u4ef6\u6587\u4ef6\u3002\u6253\u5f00\u8be5\u6587\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;?php namespace AppHttpMiddleware;\n\nuse Closure;\n\nclass MigrateMiddleware\n{\n    public function handle($request, Closure $next)\n    {\n        \/\/ \u5728\u6570\u636e\u8fc1\u79fb\u671f\u95f4\u6267\u884c\u7684\u989d\u5916\u64cd\u4f5c\uff0c\u4f8b\u5982\u5bfc\u5165\u521d\u59cb\u6570\u636e\u7b49\n\n        return $next($request);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728 handle \u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6267\u884c\u5728\u6570\u636e\u8fc1\u79fb\u671f\u95f4\u9700\u8981\u8fdb\u884c\u7684\u989d\u5916\u64cd\u4f5c\uff0c\u4f8b\u5982\u5bfc\u5165\u521d\u59cb\u6570\u636e\u7b49\u3002<\/p>\n<p>\u6b65\u9aa4\u4e09\uff1a\u6ce8\u518c\u4e2d\u95f4\u4ef6<br \/>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5c06\u4e2d\u95f4\u4ef6\u6ce8\u518c\u5230Laravel\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002\u6253\u5f00 app\/Http\/Kernel.php \u6587\u4ef6\uff0c\u5728 $routeMiddleware \u6570\u7ec4\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>protected $routeMiddleware = [\n    \/\/ \u5176\u4ed6\u4e2d\u95f4\u4ef6...\n    'migrate' =&amp;gt; AppHttpMiddlewareMigrateMiddleware::class,\n];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u5904\uff0c\u6211\u4eec\u5c06\u4e2d\u95f4\u4ef6\u547d\u540d\u4e3a migrate\uff0c\u5e76\u5c06\u5176\u6307\u5411 AppHttpMiddlewareMigrateMiddleware \u7c7b\u3002<\/p>\n<p>\u6b65\u9aa4\u56db\uff1a\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u6570\u636e\u8fc1\u79fb<br \/>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u8fc1\u79fb\u6587\u4ef6\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u6765\u6267\u884c\u989d\u5916\u7684\u64cd\u4f5c\u4e86\u3002\u6253\u5f00 create_users_table.php \u8fc1\u79fb\u6587\u4ef6\uff0c\u5e76\u5728 up \u65b9\u6cd5\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>public function up()\n{\n    Schema::create('users', function (Blueprint $table) {\n        $table-&amp;gt;id();\n        $table-&amp;gt;string('name');\n        $table-&amp;gt;string('email')-&amp;gt;unique();\n        $table-&amp;gt;timestamps();\n    });\n\n    if (app()-&amp;gt;runningInConsole()) {\n        $this-&amp;gt;call('migrate');\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u5904\uff0c\u6211\u4eec\u5728 up \u65b9\u6cd5\u4e2d\u4f7f\u7528 app()-&gt;runningInConsole() \u6765\u5224\u65ad\u5f53\u524d\u662f\u5426\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u3002\u5982\u679c\u662f\uff0c\u5219\u8c03\u7528 migrate \u547d\u4ee4\uff0c\u4ece\u800c\u6267\u884c MigrateMiddleware \u4e2d\u95f4\u4ef6\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u6b65\u9aa4\u4e94\uff1a\u8fd0\u884c\u8fc1\u79fb\u547d\u4ee4<br \/>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u8fd0\u884c\u8fc1\u79fb\u547d\u4ee4\u6765\u6267\u884c\u6570\u636e\u8fc1\u79fb\u3002\u5728Laravel\u9879\u76ee\u7684\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>php artisan migrate<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u521b\u5efa users \u8868\uff0c\u5e76\u6839\u636e\u5b9a\u4e49\u7684\u5b57\u6bb5\u521b\u5efa\u76f8\u5e94\u7684\u6570\u636e\u5e93\u8868\u7ed3\u6784\u3002<\/p>\n<p>\u603b\u7ed3<br \/>\u901a\u8fc7\u521b\u5efa\u4e2d\u95f4\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Laravel\u4e2d\u8fdb\u884c\u6570\u636e\u8fc1\u79fb\u671f\u95f4\u6267\u884c\u989d\u5916\u7684\u64cd\u4f5c\u3002\u672c\u6587\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u6570\u636e\u8fc1\u79fb\u3002\u795d\u4f60\u5728Laravel\u5f00\u53d1\u4e2d\u53d6\u5f97\u6210\u529f\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u6570\u636e\u8fc1\u79fb\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>\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u6570\u636e\u8fc1\u79fb \u7b80\u4ecb\u5728Laravel\u4e2d\uff0c\u6570\u636e\u8fc1\u79fb\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u7528\u4e8e\u7ba1\u7406\u6570\u636e\u5e93\u8868\u7ed3\u6784\u548c\u6570\u636e\u7684\u53d8\u5316\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f1a\u901a\u8fc7\u8fc1\u79fb\u6587\u4ef6\u6765\u521b\u5efa\u3001\u4fee\u6539\u548c\u5220\u9664\u6570\u636e\u5e93\u7684\u8868\u548c\u5b57\u6bb5\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5728\u6570\u636e\u8fc1\u79fb\u671f\u95f4\u6267\u884c\u4e00\u4e9b\u989d\u5916\u7684\u64cd\u4f5c\u3002\u8fd9\u65f6\uff0c\u4e2d\u95f4\u4ef6\u5c31\u53ef\u4ee5\u6d3e\u4e0a\u7528\u573a\u4e86\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5728Laravel\u4e2d\u5982\u4f55\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u6570\u636e\u8fc1\u79fb\uff0c\u5e76\u63d0\u4f9b\u8be6\u7ec6\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u6b65\u9aa4\u4e00\uff1a\u521b\u5efa\u8fc1\u79fb\u6587\u4ef6\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u8fc1\u79fb\u6587\u4ef6\uff0c\u7528\u4e8e\u5b9a\u4e49\u9700\u8981\u8fdb\u884c\u6570\u636e\u8fc1\u79fb\u7684\u6570\u636e\u5e93\u8868\u548c\u5b57\u6bb5\u3002\u901a\u8fc7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u5728Laravel\u9879\u76ee\u7684\u7ec8\u7aef\u4e2d\u521b\u5efa\u4e00\u4e2a\u8fc1\u79fb\u6587\u4ef6\uff1a php artisan make:migration create_users_table \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u5728 database\/migrations \u6587\u4ef6\u5939\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a create_users_table.php \u7684\u8fc1\u79fb\u6587\u4ef6\u3002\u6253\u5f00\u8be5\u6587\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4e0b\u4ee3\u7801\uff1a &lt;?php use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema; class CreateUsersTable extends Migration { public function up() { Schema::create(&#8216;users&#8217;, function (Blueprint $table) { $table-&gt;id(); $table-&amp;gt;string(&#8216;name&#8217;); $table-&amp;gt;string(&#8217;email&#8217;)-&amp;gt;unique(); $table-&amp;gt;timestamps(); }); } public function down() { Schema::dropIfExists(&#8216;users&#8217;); } } \u767b\u5f55\u540e\u590d\u5236 \u5728 up \u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 Schema \u7c7b\u6765\u521b\u5efa users \u8868\uff0c\u5b9a\u4e49\u4e86 [&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-24799","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24799","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=24799"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24799\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}