{"id":24900,"date":"2024-11-21T16:26:46","date_gmt":"2024-11-21T08:26:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24900\/"},"modified":"2024-11-21T16:26:46","modified_gmt":"2024-11-21T08:26:46","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%e5%af%bc%e5%85%a5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24900\/","title":{"rendered":"\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u6570\u636e\u5bfc\u5165"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169906063535822.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u6570\u636e\u5bfc\u5165\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u6570\u636e\u5bfc\u5165\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u6570\u636e\u5bfc\u5165<\/p>\n<p>\u4e2d\u95f4\u4ef6\u662fLaravel\u6846\u67b6\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u8bf7\u6c42\u7684\u751f\u547d\u5468\u671f\u4e2d\u6267\u884c\u4e00\u4e9b\u989d\u5916\u7684\u64cd\u4f5c\uff0c\u5982\u8eab\u4efd\u9a8c\u8bc1\u3001\u65e5\u5fd7\u8bb0\u5f55\u7b49\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528\u4e2d\u95f4\u4ef6\u5728Laravel\u4e2d\u8fdb\u884c\u6570\u636e\u5bfc\u5165\u64cd\u4f5c\uff0c\u5e76\u4e14\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u6765\u5904\u7406\u6570\u636e\u5bfc\u5165\u7684\u903b\u8f91\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Laravel\u7684\u547d\u4ee4\u884c\u5de5\u5177 artisan \u6765\u751f\u6210\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u6a21\u677f\u3002<\/p>\n<pre>php artisan make:middleware ImportData<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u751f\u6210\u7684\u4e2d\u95f4\u4ef6\u6587\u4ef6\u5c06\u4f4d\u4e8e app\/Http\/Middleware \u76ee\u5f55\u4e0b\uff0c\u6587\u4ef6\u540d\u4e3a ImportData.php\u3002\u6253\u5f00\u8be5\u6587\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u7c7b\u7684\u57fa\u672c\u7ed3\u6784\u3002\u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u5728 handle \u65b9\u6cd5\u4e2d\u5b9e\u73b0\u5177\u4f53\u7684\u6570\u636e\u5bfc\u5165\u903b\u8f91\u3002<\/p>\n<pre>&lt;?php namespace AppHttpMiddleware;\n\nuse Closure;\nuse IlluminateSupportFacadesDB;\n\nclass ImportData\n{\n    public function handle($request, Closure $next)\n    {\n        \/\/ \u5728\u8fd9\u91cc\u7f16\u5199\u6570\u636e\u5bfc\u5165\u7684\u903b\u8f91\n\n        \/\/ \u8bfb\u53d6 CSV \u6587\u4ef6\n        $csvFile = fopen('path\/to\/csv\/file.csv', 'r');\n\n        \/\/ \u8bfb\u53d6\u6bcf\u4e00\u884c\u6570\u636e\uff0c\u5e76\u8fdb\u884c\u5904\u7406\n        while (($data = fgetcsv($csvFile, 0, ',')) !== false) {\n            \/\/ \u5728\u8fd9\u91cc\u5bf9\u5bfc\u5165\u7684\u6570\u636e\u505a\u4e9b\u5904\u7406\uff0c\u5982\u9a8c\u8bc1\u3001\u683c\u5f0f\u8f6c\u6362\u7b49\n\n            \/\/ \u63d2\u5165\u6570\u636e\u81f3\u6570\u636e\u5e93\n            DB::table('your_table')-&gt;insert([\n                'column1' =&amp;gt; $data[0],\n                'column2' =&amp;gt; $data[1],\n                \/\/ ...\n            ]);\n        }\n\n        \/\/ \u5173\u95ed CSV \u6587\u4ef6\n        fclose($csvFile);\n\n        return $next($request);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 PHP \u7684\u5185\u7f6e\u51fd\u6570 fopen \u6765\u6253\u5f00 CSV \u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 fgetcsv \u51fd\u6570\u9010\u884c\u8bfb\u53d6\u6570\u636e\u3002\u8fd9\u91cc\u7684\u8def\u5f84 &#8216;path\/to\/csv\/file.csv&#8217; \u9700\u8981\u66ff\u6362\u4e3a\u4f60\u81ea\u5df1\u7684 CSV \u6587\u4ef6\u8def\u5f84\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u6bcf\u4e00\u884c\u6570\u636e\u8fdb\u884c\u5904\u7406\uff0c\u5982\u9a8c\u8bc1\u3001\u683c\u5f0f\u8f6c\u6362\u7b49\uff0c\u518d\u4f7f\u7528 Laravel \u63d0\u4f9b\u7684 DB \u7c7b\u6765\u5c06\u6570\u636e\u63d2\u5165\u6570\u636e\u5e93\u4e2d\u3002\u6700\u540e\uff0c\u5173\u95ed CSV \u6587\u4ef6\u5e76\u8c03\u7528 $next($request) \u5c06\u8bf7\u6c42\u4f20\u9012\u7ed9\u4e0b\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u6216\u8def\u7531\u5904\u7406\u7a0b\u5e8f\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5c06\u4e2d\u95f4\u4ef6\u5e94\u7528\u5230\u76f8\u5e94\u7684\u8def\u7531\u4e0a\u3002\u6253\u5f00 app\/Http\/Kernel.php \u6587\u4ef6\uff0c\u627e\u5230 $middlewareGroups \u5c5e\u6027\uff0c\u5e76\u5728 web \u7ec4\u4e2d\u5c06\u6211\u4eec\u521b\u5efa\u7684\u4e2d\u95f4\u4ef6\u6dfb\u52a0\u8fdb\u53bb\u3002<\/p>\n<pre>protected $middlewareGroups = [\n    \/\/ ...\n    'web' =&amp;gt; [\n        \/\/ ...\n        AppHttpMiddlewareImportData::class,\n    ],\n    \/\/ ...\n];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\uff0c\u5f53\u6211\u4eec\u8bbf\u95ee\u4f7f\u7528 web \u4e2d\u95f4\u4ef6\u7ec4\u7684\u8def\u7531\u65f6\uff0c\u4e2d\u95f4\u4ef6\u5c06\u88ab\u6267\u884c\uff0c\u5e76\u5904\u7406\u6570\u636e\u5bfc\u5165\u7684\u903b\u8f91\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91cc\u7684\u6570\u636e\u5bfc\u5165\u903b\u8f91\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5b9e\u9645\u9879\u76ee\u4e2d\u53ef\u80fd\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u8c03\u6574\u548c\u62d3\u5c55\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u9a8c\u8bc1\u903b\u8f91\u6765\u786e\u4fdd\u5bfc\u5165\u6570\u636e\u7684\u51c6\u786e\u6027\u548c\u5b8c\u6574\u6027\uff0c\u6216\u8005\u6dfb\u52a0\u9519\u8bef\u5904\u7406\u903b\u8f91\u6765\u5904\u7406\u5bfc\u5165\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\u7b49\u7b49\u3002<\/p>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u4e86\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u6570\u636e\u5bfc\u5165\u64cd\u4f5c\u3002\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u7c7b\u5e76\u5728\u5176\u4e2d\u5b9e\u73b0\u5177\u4f53\u7684\u6570\u636e\u5bfc\u5165\u903b\u8f91\uff0c\u7136\u540e\u5c06\u4e2d\u95f4\u4ef6\u5e94\u7528\u5230\u76f8\u5e94\u7684\u8def\u7531\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u8bf7\u6c42\u7684\u751f\u547d\u5468\u671f\u4e2d\u6267\u884c\u6570\u636e\u5bfc\u5165\u64cd\u4f5c\u3002<\/p>\n<p>\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u7406\u89e3\u548c\u5e94\u7528Laravel\u4e2d\u95f4\u4ef6\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u6570\u636e\u5bfc\u5165\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\u5bfc\u5165 \u4e2d\u95f4\u4ef6\u662fLaravel\u6846\u67b6\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u8bf7\u6c42\u7684\u751f\u547d\u5468\u671f\u4e2d\u6267\u884c\u4e00\u4e9b\u989d\u5916\u7684\u64cd\u4f5c\uff0c\u5982\u8eab\u4efd\u9a8c\u8bc1\u3001\u65e5\u5fd7\u8bb0\u5f55\u7b49\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528\u4e2d\u95f4\u4ef6\u5728Laravel\u4e2d\u8fdb\u884c\u6570\u636e\u5bfc\u5165\u64cd\u4f5c\uff0c\u5e76\u4e14\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u6765\u5904\u7406\u6570\u636e\u5bfc\u5165\u7684\u903b\u8f91\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Laravel\u7684\u547d\u4ee4\u884c\u5de5\u5177 artisan \u6765\u751f\u6210\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u6a21\u677f\u3002 php artisan make:middleware ImportData \u767b\u5f55\u540e\u590d\u5236 \u751f\u6210\u7684\u4e2d\u95f4\u4ef6\u6587\u4ef6\u5c06\u4f4d\u4e8e app\/Http\/Middleware \u76ee\u5f55\u4e0b\uff0c\u6587\u4ef6\u540d\u4e3a ImportData.php\u3002\u6253\u5f00\u8be5\u6587\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u7c7b\u7684\u57fa\u672c\u7ed3\u6784\u3002\u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u5728 handle \u65b9\u6cd5\u4e2d\u5b9e\u73b0\u5177\u4f53\u7684\u6570\u636e\u5bfc\u5165\u903b\u8f91\u3002 &lt;?php namespace AppHttpMiddleware; use Closure; use IlluminateSupportFacadesDB; class ImportData { public function handle($request, Closure $next) { \/\/ \u5728\u8fd9\u91cc\u7f16\u5199\u6570\u636e\u5bfc\u5165\u7684\u903b\u8f91 \/\/ \u8bfb\u53d6 CSV \u6587\u4ef6 $csvFile = fopen(&#8216;path\/to\/csv\/file.csv&#8217;, &#8216;r&#8217;); \/\/ \u8bfb\u53d6\u6bcf\u4e00\u884c\u6570\u636e\uff0c\u5e76\u8fdb\u884c\u5904\u7406 while (($data = fgetcsv($csvFile, 0, &#8216;,&#8217;)) !== false) { \/\/ \u5728\u8fd9\u91cc\u5bf9\u5bfc\u5165\u7684\u6570\u636e\u505a\u4e9b\u5904\u7406\uff0c\u5982\u9a8c\u8bc1\u3001\u683c\u5f0f\u8f6c\u6362\u7b49 [&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-24900","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24900","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=24900"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24900\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}