{"id":22374,"date":"2024-11-21T08:50:49","date_gmt":"2024-11-21T00:50:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22374\/"},"modified":"2024-11-21T08:50:49","modified_gmt":"2024-11-21T00:50:49","slug":"laravel%e6%95%b0%e6%8d%ae%e8%a1%a8%e6%80%8e%e4%b9%88%e5%88%a0%e9%99%a4","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22374\/","title":{"rendered":"laravel\u6570\u636e\u8868\u600e\u4e48\u5220\u9664"},"content":{"rendered":"<p>laravel\u662f\u4e00\u4e2a\u5e7f\u53d7\u6b22\u8fce\u7684php web\u5e94\u7528\u7a0b\u5e8f\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u529f\u80fd\uff0c\u4f7f\u5f97web\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u548c\u9ad8\u6548\u3002\u5176\u4e2d\u4e00\u4e2a\u91cd\u8981\u7684\u529f\u80fd\u662f\u5904\u7406\u6570\u636e\u5e93\u6570\u636e\uff0c\u5305\u62ec\u6570\u636e\u8868\u7684\u5220\u9664\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u5728laravel\u4e2d\u5220\u9664\u6570\u636e\u8868\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<ol>\n<li>Laravel\u4e2d\u7684\u6570\u636e\u5e93\u8fc1\u79fb<\/li>\n<\/ol>\n<p>\u5728Laravel\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6570\u636e\u5e93\u8fc1\u79fb\u6765\u521b\u5efa\u3001\u4fee\u6539\u6216\u5220\u9664\u6570\u636e\u8868\u3002\u6570\u636e\u5e93\u8fc1\u79fb\u662fLaravel\u63d0\u4f9b\u7684\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u60a8\u7f16\u5199PHP\u4ee3\u7801\uff0c\u4ee5\u4fbf\u8f7b\u677e\u5730\u521b\u5efa\u3001\u4fee\u6539\u6216\u5220\u9664\u6570\u636e\u8868\uff0c\u800c\u4e0d\u5fc5\u624b\u52a8\u7f16\u5199SQL\u8bed\u53e5\u3002<\/p>\n<p>\u8981\u521b\u5efa\u4e00\u4e2aLaravel\u8fc1\u79fb\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Artisan\u547d\u4ee4\u884c\u5de5\u5177\u3002\u5728\u547d\u4ee4\u884c\u4e2d\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>php artisan make:migration create_example_table<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201ccreate_example_table\u201d\u7684\u8fc1\u79fb\u3002\u8fd9\u5c06\u5728databasemigrations\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fc1\u79fb\u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u6587\u4ef6\u4e2d\u4f7f\u7528Laravel\u63d0\u4f9b\u7684Schema\u7c7b\u6765\u5b9a\u4e49\u6570\u636e\u8868\u7684\u7ed3\u6784\u548c\u5b57\u6bb5\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>use IlluminateDatabaseMigrationsMigration;\nuse IlluminateDatabaseSchemaBlueprint;\nuse IlluminateSupportFacadesSchema;\n\nclass CreateExampleTable extends Migration\n{\n    \/**\n     * Run the migrations.\n     *\n     * @return void\n     *\/\n    public function up()\n    {\n        Schema::create('example', function (Blueprint $table) {\n            $table-&gt;id();\n            $table-&gt;string('title');\n            $table-&gt;text('content');\n            $table-&gt;timestamps();\n        });\n    }\n\n    \/**\n     * Reverse the migrations.\n     *\n     * @return void\n     *\/\n    public function down()\n    {\n        Schema::dropIfExists('example');\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a\u201cexample\u201d\u7684\u6570\u636e\u8868\uff0c\u5b83\u6709\u4e00\u4e2a\u81ea\u589e\u7684ID\u5b57\u6bb5\u3001\u4e00\u4e2a\u6807\u9898\u5b57\u6bb5\u3001\u4e00\u4e2a\u5185\u5bb9\u5b57\u6bb5\u548c\u65f6\u95f4\u6233\u5b57\u6bb5\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u4e5f\u5b9a\u4e49\u4e86reverse\u65b9\u6cd5down()\uff0c\u4ee5\u4fbf\u5728\u9700\u8981\u65f6\u53ef\u4ee5\u64a4\u9500\u8be5\u8fc1\u79fb\u3002<\/p>\n<ol>\n<li>\u5220\u9664\u6570\u636e\u8868<\/li>\n<\/ol>\n<p>\u5728Laravel\u4e2d\u5220\u9664\u6570\u636e\u8868\u975e\u5e38\u7b80\u5355\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Artisan\u547d\u4ee4\u884c\u5de5\u5177\u7684migrate:rollback\u547d\u4ee4\u3002\u8fd9\u4e2a\u547d\u4ee4\u5c06\u56de\u6eda\u6700\u540e\u4e00\u4e2a\u8fc1\u79fb\u6587\u4ef6\uff0c\u4e5f\u5c31\u662f\u521a\u521a\u521b\u5efa\u7684\u201ccreate_example_table\u201d\u8fc1\u79fb\u3002<\/p>\n<p>\u5728\u547d\u4ee4\u884c\u4e2d\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>php artisan migrate:rollback<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u547d\u4ee4\u5c06\u56de\u6eda\u201ccreate_example_table\u201d\u8fc1\u79fb\u6587\u4ef6\uff0c\u5e76\u5220\u9664\u201cexample\u201d\u6570\u636e\u8868\u53ca\u5176\u6240\u6709\u6570\u636e\u3002<\/p>\n<p>\u6ce8\u610f\uff0c\u5982\u679c\u60a8\u60f3\u5220\u9664\u66f4\u591a\u7684\u6570\u636e\u8868\uff0c\u60a8\u53ef\u4ee5\u56de\u6eda\u5230\u9700\u8981\u5220\u9664\u7684\u6700\u540e\u4e00\u4e2a\u8fc1\u79fb\u6587\u4ef6\uff0c\u7136\u540e\u5c06down()\u65b9\u6cd5\u4e2d\u7684\u76f8\u5e94dropIfExists()\u51fd\u6570\u53d6\u6d88\u6ce8\u91ca\u3002<\/p>\n<ol>\n<li>\u5220\u9664\u6570\u636e\u8868\u4f46\u4fdd\u7559\u6570\u636e<\/li>\n<\/ol>\n<p>\u5982\u679c\u60a8\u60f3\u4fdd\u7559\u6570\u636e\u5e76\u5220\u9664\u6570\u636e\u8868\uff0cLaravel\u63d0\u4f9b\u4e86\u53e6\u4e00\u79cd\u529f\u80fd\uff0c\u5373\u5220\u9664\u6570\u636e\u8868\u4f46\u4fdd\u7559\u6570\u636e\u7684\u8fc1\u79fb\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Artisan\u547d\u4ee4\u884c\u5de5\u5177\u7684make:drop-pretend\u547d\u4ee4\u6765\u521b\u5efa\u8fd9\u79cd\u7c7b\u578b\u7684\u8fc1\u79fb\u3002<\/p>\n<p>\u5728\u547d\u4ee4\u884c\u4e2d\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>php artisan make:drop-pretend example<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cdrop_example_table\u201d\u7684\u8fc1\u79fb\u6587\u4ef6\u3002\u8fd9\u5c06\u5728databasemigrations\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fc1\u79fb\u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u6587\u4ef6\u4e2d\u4f7f\u7528Laravel\u63d0\u4f9b\u7684Schema\u7c7b\u6765\u5b9a\u4e49\u8981\u5220\u9664\u7684\u6570\u636e\u8868\u548c\u5185\u5bb9\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>use IlluminateDatabaseMigrationsMigration;\nuse IlluminateSupportFacadesSchema;\n\nclass DropExampleTable extends Migration\n{\n    \/**\n     * Run the migrations.\n     *\n     * @return void\n     *\/\n    public function up()\n    {\n        Schema::table('example', function ($table) {\n            DB::statement('SET FOREIGN_KEY_CHECKS=0;');\n            $table-&gt;drop();\n            DB::statement('SET FOREIGN_KEY_CHECKS=1;');\n        });\n    }\n\n    \/**\n     * Reverse the migrations.\n     *\n     * @return void\n     *\/\n    public function down()\n    {\n        \/\/ This migration cannot be reversed.\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86table()\u65b9\u6cd5\u5e76\u4f20\u9012\u4e86\u6570\u636e\u8868\u540d\u6765\u5b9a\u4e49\u8981\u5220\u9664\u7684\u6570\u636e\u8868\u3002\u7136\u540e\uff0c\u5728up()\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86drop()\u65b9\u6cd5\u6765\u5220\u9664\u6570\u636e\u8868\u3002\u4e3a\u4e86\u907f\u514d\u7531\u4e8e\u5916\u952e\u7ea6\u675f\u800c\u5f15\u53d1\u7684\u9519\u8bef\uff0c\u6211\u4eec\u4f7f\u7528\u4e86DB::statement()\u51fd\u6570\u6765\u5173\u95ed\u548c\u6253\u5f00MySQL\u7684\u5916\u952e\u7ea6\u675f\u68c0\u67e5\u3002\u5728down()\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u6ca1\u6709\u5b9a\u4e49\u4efb\u4f55\u5185\u5bb9\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u8fc1\u79fb\u662f\u4e0d\u80fd\u88ab\u53cd\u8f6c\u7684\u3002<\/p>\n<ol>\n<li>\u603b\u7ed3<\/li>\n<\/ol>\n<p>\u5728Laravel\u4e2d\uff0c\u5904\u7406\u6570\u636e\u5e93\u6570\u636e\u662fWeb\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u7684\u91cd\u8981\u90e8\u5206\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u4f7f\u7528Laravel\u6570\u636e\u5e93\u8fc1\u79fb\u6765\u5220\u9664\u6570\u636e\u8868\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u4f7f\u7528Artisan\u547d\u4ee4\u884c\u5de5\u5177\u6765\u521b\u5efa\u548c\u56de\u6eda\u8fc1\u79fb\u6587\u4ef6\uff0c\u540c\u65f6\u4fdd\u8bc1\u4e86\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u3002\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528\u5220\u9664\u6570\u636e\u8868\u4f46\u4fdd\u7559\u6570\u636e\u7684\u8fc1\u79fb\u3002\u8fd9\u4e9b\u90fd\u662fLaravel\u4e2d\u975e\u5e38\u5b9e\u7528\u7684\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Laravel\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u4e2d\u4f7f\u7528\u5b83\u4eec\u6765\u5904\u7406\u6570\u636e\u5e93\u6570\u636e\uff0c\u4ee5\u4fbf\u5f00\u53d1\u66f4\u52a0\u9ad8\u6548\u548c\u65b9\u4fbf\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel\u6570\u636e\u8868\u600e\u4e48\u5220\u9664\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\u4e2a\u5e7f\u53d7\u6b22\u8fce\u7684php web\u5e94\u7528\u7a0b\u5e8f\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u529f\u80fd\uff0c\u4f7f\u5f97web\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u548c\u9ad8\u6548\u3002\u5176\u4e2d\u4e00\u4e2a\u91cd\u8981\u7684\u529f\u80fd\u662f\u5904\u7406\u6570\u636e\u5e93\u6570\u636e\uff0c\u5305\u62ec\u6570\u636e\u8868\u7684\u5220\u9664\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u5728laravel\u4e2d\u5220\u9664\u6570\u636e\u8868\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002 Laravel\u4e2d\u7684\u6570\u636e\u5e93\u8fc1\u79fb \u5728Laravel\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6570\u636e\u5e93\u8fc1\u79fb\u6765\u521b\u5efa\u3001\u4fee\u6539\u6216\u5220\u9664\u6570\u636e\u8868\u3002\u6570\u636e\u5e93\u8fc1\u79fb\u662fLaravel\u63d0\u4f9b\u7684\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u60a8\u7f16\u5199PHP\u4ee3\u7801\uff0c\u4ee5\u4fbf\u8f7b\u677e\u5730\u521b\u5efa\u3001\u4fee\u6539\u6216\u5220\u9664\u6570\u636e\u8868\uff0c\u800c\u4e0d\u5fc5\u624b\u52a8\u7f16\u5199SQL\u8bed\u53e5\u3002 \u8981\u521b\u5efa\u4e00\u4e2aLaravel\u8fc1\u79fb\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Artisan\u547d\u4ee4\u884c\u5de5\u5177\u3002\u5728\u547d\u4ee4\u884c\u4e2d\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a php artisan make:migration create_example_table \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201ccreate_example_table\u201d\u7684\u8fc1\u79fb\u3002\u8fd9\u5c06\u5728databasemigrations\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fc1\u79fb\u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u6587\u4ef6\u4e2d\u4f7f\u7528Laravel\u63d0\u4f9b\u7684Schema\u7c7b\u6765\u5b9a\u4e49\u6570\u636e\u8868\u7684\u7ed3\u6784\u548c\u5b57\u6bb5\uff0c\u5982\u4e0b\u6240\u793a\uff1a use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema; class CreateExampleTable extends Migration { \/** * Run the migrations. * * @return void *\/ public function up() { Schema::create(&#8216;example&#8217;, function (Blueprint $table) { $table-&gt;id(); $table-&gt;string(&#8216;title&#8217;); $table-&gt;text(&#8216;content&#8217;); $table-&gt;timestamps(); }); } \/** * Reverse the migrations. * * @return [&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-22374","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22374","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=22374"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22374\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}