{"id":24817,"date":"2024-11-21T16:27:01","date_gmt":"2024-11-21T08:27:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24817\/"},"modified":"2024-11-21T16:27:01","modified_gmt":"2024-11-21T08:27:01","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8laravel%e5%ae%9e%e7%8e%b0%e6%95%b0%e6%8d%ae%e5%af%bc%e5%85%a5%e5%92%8c%e5%af%bc%e5%87%ba%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24817\/","title":{"rendered":"\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u5bfc\u5165\u548c\u5bfc\u51fa\u529f\u80fd"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169889592889296.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u5bfc\u5165\u548c\u5bfc\u51fa\u529f\u80fd\u63d2\u56fe\" alt=\"\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u5bfc\u5165\u548c\u5bfc\u51fa\u529f\u80fd\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u5bfc\u5165\u548c\u5bfc\u51fa\u529f\u80fd<\/p>\n<p>\u5bfc\u5165\u548c\u5bfc\u51fa\u6570\u636e\u662fWeb\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5e38\u89c1\u7684\u529f\u80fd\u4e4b\u4e00\u3002\u77e5\u9053\u5982\u4f55\u4f7f\u7528Laravel\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u5c06\u786e\u4fdd\u4f60\u80fd\u591f\u8f7b\u677e\u5730\u5904\u7406\u5927\u91cf\u7684\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u53ef\u7528\u7684\u683c\u5f0f\u3002<\/p>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Laravel\u6765\u5b9e\u73b0\u6570\u636e\u7684\u5bfc\u5165\u548c\u5bfc\u51fa\u529f\u80fd\u3002\u6211\u4eec\u5c06\u8986\u76d6\u4e24\u4e2a\u65b9\u9762\uff1a\u6570\u636e\u5bfc\u5165\u548c\u6570\u636e\u5bfc\u51fa\u3002\u6211\u4eec\u8fd8\u5c06\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u5e2e\u52a9\u4f60\u7406\u89e3\u8fd9\u4e2a\u8fc7\u7a0b\u3002<\/p>\n<p>\u6570\u636e\u5bfc\u5165<\/p>\n<p>\u6570\u636e\u5bfc\u5165\u662f\u6307\u5c06\u5916\u90e8\u6570\u636e\u5bfc\u5165\u5230\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u8868\u6765\u5b58\u50a8\u5bfc\u5165\u7684\u6570\u636e\u3002\u5728Laravel\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fc1\u79fb\u6765\u521b\u5efa\u8868\uff1a<\/p>\n<pre>php artisan make:migration create_import_data_table --create=import_data<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u5728database\/migrations\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u8fc1\u79fb\u6587\u4ef6\u3002\u5728\u8be5\u8fc1\u79fb\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u8868\u7684\u7ed3\u6784\u548c\u5b57\u6bb5\uff1a<\/p>\n<pre>\/\/ database\/migrations\/YYYY_MM_DD_HHmmss_create_import_data_table.php\n\nuse IlluminateDatabaseMigrationsMigration;\nuse IlluminateDatabaseSchemaBlueprint;\nuse IlluminateSupportFacadesSchema;\n\nclass CreateImportDataTable extends Migration\n{\n    public function up()\n    {\n        Schema::create('import_data', function (Blueprint $table) {\n            $table-&amp;gt;id();\n            $table-&amp;gt;string('name');\n            $table-&amp;gt;string('email');\n            $table-&amp;gt;timestamps();\n        });\n    }\n\n    public function down()\n    {\n        Schema::dropIfExists('import_data');\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fc1\u79fb\u6587\u4ef6\u4e2d\u5b9a\u4e49\u597d\u8868\u7684\u7ed3\u6784\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd0\u884c\u8fc1\u79fb\u547d\u4ee4\u6765\u521b\u5efa\u8868\uff1a<\/p>\n<pre>php artisan migrate<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u63a7\u5236\u5668\u6765\u5904\u7406\u5bfc\u5165\u6570\u636e\u7684\u903b\u8f91\u3002\u5047\u8bbe\u6211\u4eec\u7684\u5bfc\u5165\u6570\u636e\u662f\u4ee5CSV\u6587\u4ef6\u7684\u5f62\u5f0f\u63d0\u4f9b\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Laravel\u7684Storage\u7c7b\u6765\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\uff1a<\/p>\n<pre>namespace AppHttpControllers;\n\nuse IlluminateHttpRequest;\nuse IlluminateSupportFacadesStorage;\n\nclass ImportDataController extends Controller\n{\n    public function import(Request $request)\n    {\n        $file = $request-&amp;gt;file('file');\n\n        if ($file) {\n            $filePath = $file-&amp;gt;store('import');\n            \n            \/\/ \u89e3\u6790CSV\u6587\u4ef6\u5e76\u63d2\u5165\u6570\u636e\u5e93\n            $csvData = file_get_contents(Storage::path($filePath));\n            $lines = explode(PHP_EOL, $csvData);\n            \n            foreach ($lines as $line) {\n                $data = str_getcsv($line);\n\n                \/\/ \u5728\u8fd9\u91cc\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\u548c\u63d2\u5165\u6570\u636e\u5e93\u64cd\u4f5c\n            }\n\n            return 'Data imported successfully!';\n        }\n\n        return 'No file uploaded.';\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u68c0\u67e5\u662f\u5426\u6709\u6587\u4ef6\u4e0a\u4f20\u3002\u5982\u679c\u6709\u6587\u4ef6\u4e0a\u4f20\uff0c\u6211\u4eec\u5c06\u5176\u5b58\u50a8\u5728storage\/app\/import\u76ee\u5f55\u4e0b\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528file_get_contents\u51fd\u6570\u4eceCSV\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u5e76\u4f7f\u7528explode\u51fd\u6570\u5c06\u5176\u5206\u5272\u4e3a\u884c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528str_getcsv\u51fd\u6570\u89e3\u6790\u6bcf\u4e00\u884c\u7684\u6570\u636e\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5e76\u6ca1\u6709\u5b9e\u9645\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\u548c\u63d2\u5165\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u9700\u6c42\u6765\u81ea\u5b9a\u4e49\u8fd9\u90e8\u5206\u7684\u903b\u8f91\u3002<\/p>\n<p>\u6570\u636e\u5bfc\u51fa<\/p>\n<p>\u6570\u636e\u5bfc\u51fa\u662f\u6307\u5c06\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u6570\u636e\u5bfc\u51fa\u5230\u5916\u90e8\u6587\u4ef6\u4e2d\u3002Laravel\u63d0\u4f9b\u4e86\u591a\u79cd\u683c\u5f0f\u7684\u652f\u6301\uff0c\u5305\u62ecCSV\u3001Excel\u3001JSON\u7b49\u3002\u8fd9\u91cc\u6211\u4eec\u4ee5\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\u4e3a\u4f8b\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u63a7\u5236\u5668\u6765\u5904\u7406\u5bfc\u51fa\u6570\u636e\u7684\u903b\u8f91\u3002\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5047\u8bbe\u6211\u4eec\u5c06\u5bfc\u51faimport_data\u8868\u4e2d\u7684\u6570\u636e\u4e3aCSV\u6587\u4ef6\uff1a<\/p>\n<pre>namespace AppHttpControllers;\n\nuse AppModelsImportData;\nuse IlluminateHttpRequest;\nuse IlluminateSupportFacadesStorage;\n\nclass ExportDataController extends Controller\n{\n    public function export(Request $request)\n    {\n        $filename = 'export_data.csv';\n\n        $data = ImportData::all();\n\n        $csvData = \"name,email\n\";\n\n        foreach ($data as $row) {\n            $csvData .= $row-&amp;gt;name . ',' . $row-&amp;gt;email . \"\n\";\n        }\n\n        Storage::put($filename, $csvData);\n\n        return response()-&amp;gt;download(storage_path(\"app\/{$filename}\"));\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u8981\u5bfc\u51fa\u7684\u6587\u4ef6\u540d\u548cCSV\u6587\u4ef6\u7684\u6807\u9898\u884c\u3002\u7136\u540e\uff0c\u6211\u4eec\u4eceimport_data\u8868\u4e2d\u83b7\u53d6\u6240\u6709\u6570\u636e\uff0c\u5e76\u5c06\u5176\u904d\u5386\uff0c\u5c06\u6bcf\u4e00\u884c\u6570\u636e\u6dfb\u52a0\u5230CSV\u6570\u636e\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528Storage\u7c7b\u7684put\u65b9\u6cd5\u5c06CSV\u6570\u636e\u4fdd\u5b58\u4e3a\u6587\u4ef6\u3002<\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86response()-&gt;download\u65b9\u6cd5\u6765\u4e0b\u8f7d\u5bfc\u51fa\u7684\u6587\u4ef6\u3002\u4f60\u4e5f\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u9700\u6c42\u6765\u81ea\u5b9a\u4e49\u4e0b\u8f7d\u7684\u903b\u8f91\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u5728\u8def\u7531\u4e2d\u5b9a\u4e49\u8fd9\u4e24\u4e2a\u63a7\u5236\u5668\u7684\u8def\u7531\uff1a<\/p>\n<pre>\/\/ routes\/web.php\n\nuse AppHttpControllersImportDataController;\nuse AppHttpControllersExportDataController;\n\nRoute::post('\/import', [ImportDataController::class, 'import']);\nRoute::get('\/export', [ExportDataController::class, 'export']);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e24\u4e2a\u8def\u7531\u6765\u5bfc\u5165\u548c\u5bfc\u51fa\u6570\u636e\u4e86\u3002\u4f8b\u5982\uff0c\u4f7f\u7528POST\u8bf7\u6c42\u6765\u5bfc\u5165\u6570\u636e\uff1a<\/p>\n<pre>POST \/import<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f7f\u7528GET\u8bf7\u6c42\u6765\u5bfc\u51fa\u6570\u636e\uff1a<\/p>\n<pre>GET \/export<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u5df2\u7ecf\u6210\u529f\u5730\u4f7f\u7528Laravel\u5b9e\u73b0\u4e86\u6570\u636e\u5bfc\u5165\u548c\u5bfc\u51fa\u7684\u529f\u80fd\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u6765\u8fdb\u884c\u6269\u5c55\u548c\u4f18\u5316\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u5bfc\u5165\u548c\u5bfc\u51fa\u529f\u80fd\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\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u5bfc\u5165\u548c\u5bfc\u51fa\u529f\u80fd \u5bfc\u5165\u548c\u5bfc\u51fa\u6570\u636e\u662fWeb\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5e38\u89c1\u7684\u529f\u80fd\u4e4b\u4e00\u3002\u77e5\u9053\u5982\u4f55\u4f7f\u7528Laravel\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u5c06\u786e\u4fdd\u4f60\u80fd\u591f\u8f7b\u677e\u5730\u5904\u7406\u5927\u91cf\u7684\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u53ef\u7528\u7684\u683c\u5f0f\u3002 \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Laravel\u6765\u5b9e\u73b0\u6570\u636e\u7684\u5bfc\u5165\u548c\u5bfc\u51fa\u529f\u80fd\u3002\u6211\u4eec\u5c06\u8986\u76d6\u4e24\u4e2a\u65b9\u9762\uff1a\u6570\u636e\u5bfc\u5165\u548c\u6570\u636e\u5bfc\u51fa\u3002\u6211\u4eec\u8fd8\u5c06\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u5e2e\u52a9\u4f60\u7406\u89e3\u8fd9\u4e2a\u8fc7\u7a0b\u3002 \u6570\u636e\u5bfc\u5165 \u6570\u636e\u5bfc\u5165\u662f\u6307\u5c06\u5916\u90e8\u6570\u636e\u5bfc\u5165\u5230\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u8868\u6765\u5b58\u50a8\u5bfc\u5165\u7684\u6570\u636e\u3002\u5728Laravel\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fc1\u79fb\u6765\u521b\u5efa\u8868\uff1a php artisan make:migration create_import_data_table &#8211;create=import_data \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u5728database\/migrations\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u8fc1\u79fb\u6587\u4ef6\u3002\u5728\u8be5\u8fc1\u79fb\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u8868\u7684\u7ed3\u6784\u548c\u5b57\u6bb5\uff1a \/\/ database\/migrations\/YYYY_MM_DD_HHmmss_create_import_data_table.php use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema; class CreateImportDataTable extends Migration { public function up() { Schema::create(&#8216;import_data&#8217;, function (Blueprint $table) { $table-&amp;gt;id(); $table-&amp;gt;string(&#8216;name&#8217;); $table-&amp;gt;string(&#8217;email&#8217;); $table-&amp;gt;timestamps(); }); } public function down() { Schema::dropIfExists(&#8216;import_data&#8217;); } } \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fc1\u79fb\u6587\u4ef6\u4e2d\u5b9a\u4e49\u597d\u8868\u7684\u7ed3\u6784\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd0\u884c\u8fc1\u79fb\u547d\u4ee4\u6765\u521b\u5efa\u8868\uff1a php artisan migrate \u767b\u5f55\u540e\u590d\u5236 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u63a7\u5236\u5668\u6765\u5904\u7406\u5bfc\u5165\u6570\u636e\u7684\u903b\u8f91\u3002\u5047\u8bbe\u6211\u4eec\u7684\u5bfc\u5165\u6570\u636e\u662f\u4ee5CSV\u6587\u4ef6\u7684\u5f62\u5f0f\u63d0\u4f9b\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Laravel\u7684Storage\u7c7b\u6765\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\uff1a namespace [&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-24817","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24817","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=24817"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24817\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}