{"id":48506,"date":"2024-12-02T08:20:14","date_gmt":"2024-12-02T00:20:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48506\/"},"modified":"2024-12-02T08:20:14","modified_gmt":"2024-12-02T00:20:14","slug":"%e7%90%86%e8%a7%a3-laravel-11-%e4%b8%ad-pluck-%e5%92%8c-select-%e4%b9%8b%e9%97%b4%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48506\/","title":{"rendered":"\u7406\u89e3 Laravel 11 \u4e2d pluck() \u548c select() \u4e4b\u95f4\u7684\u533a\u522b"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u7406\u89e3 Laravel 11 \u4e2d pluck() \u548c select() \u4e4b\u95f4\u7684\u533a\u522b<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300a\u7406\u89e3 Laravel 11 \u4e2d pluck() \u548c select() \u4e4b\u95f4\u7684\u533a\u522b\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730039248671e4dd072fad.jpg\" class=\"aligncenter\" title=\"\u7406\u89e3 Laravel 11 \u4e2d pluck() \u548c select() \u4e4b\u95f4\u7684\u533a\u522b\u63d2\u56fe\" alt=\"\u7406\u89e3 Laravel 11 \u4e2d pluck() \u548c select() \u4e4b\u95f4\u7684\u533a\u522b\u63d2\u56fe\" \/><\/p>\n<p>laravel \u662f\u6700\u6d41\u884c\u7684 php \u6846\u67b6\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\u3002\u5176\u4e2d\uff0cpluck() \u548c select() \u5728\u5904\u7406\u96c6\u5408\u65f6\u7ecf\u5e38\u4f7f\u7528\u3002\u5c3d\u7ba1\u5b83\u4eec\u770b\u8d77\u6765\u76f8\u4f3c\uff0c\u4f46\u5b83\u4eec\u7684\u76ee\u7684\u4e0d\u540c\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u8fd9\u4e24\u79cd\u65b9\u6cd5\u4e4b\u95f4\u7684\u5dee\u5f02\uff0c\u89e3\u91ca\u4f55\u65f6\u4f7f\u7528\u6bcf\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5b9e\u9645\u7684\u7f16\u7801\u793a\u4f8b\u6765\u6f14\u793a\u5b83\u4eec\u5728 laravel 11 \u4e2d\u7684\u7528\u6cd5\u3002<\/p>\n<h2> \u4ec0\u4e48\u662f pluck()\uff1f <\/h2>\n<p>pluck() \u65b9\u6cd5\u65e8\u5728\u4ece\u96c6\u5408\u4e2d\u7684\u5355\u4e2a\u952e\u4e2d\u63d0\u53d6\u503c\u3002\u5f53\u60a8\u60f3\u8981\u4ece\u6570\u7ec4\u6216\u5bf9\u8c61\u96c6\u5408\u4e2d\u68c0\u7d22\u7279\u5b9a\u5c5e\u6027\u65f6\uff0c\u5b83\u7279\u522b\u65b9\u4fbf\u3002<\/p>\n<h3> pluck() \u7684\u793a\u4f8b <\/h3>\n<p>\u5047\u8bbe\u60a8\u6709\u4e00\u7cfb\u5217\u4ea7\u54c1\uff0c\u5e76\u4e14\u60a8\u53ea\u60f3\u63d0\u53d6\u4ea7\u54c1\u540d\u79f0\uff1a<\/p>\n<pre>$collection = collect([\n    ['product_id' =&gt; 'prod-100', 'name' =&gt; 'desk'],\n    ['product_id' =&gt; 'prod-200', 'name' =&gt; 'chair'],\n]);\n\n\/\/ pluck only the names of the products\n$plucked = $collection-&gt;pluck('name');\n\n$plucked-&gt;all();\n\/\/ output: ['desk', 'chair']\n<\/pre>\n<p>\u6b64\u5916\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 pluck() \u5c06\u81ea\u5b9a\u4e49\u952e\u5206\u914d\u7ed9\u7ed3\u679c\u96c6\u5408\uff1a<\/p>\n<pre>$plucked = $collection-&gt;pluck('name', 'product_id');\n\n$plucked-&gt;all();\n\/\/ output: ['prod-100' =&gt; 'desk', 'prod-200' =&gt; 'chair']\n<\/pre>\n<h3> \u4f7f\u7528 pluck() \u5d4c\u5957\u503c <\/h3>\n<p>pluck() \u65b9\u6cd5\u8fd8\u652f\u6301\u4f7f\u7528\u70b9\u8868\u793a\u6cd5\u63d0\u53d6\u5d4c\u5957\u503c\uff1a<\/p>\n<pre>$collection = collect([\n    [\n        'name' =&gt; 'laracon',\n        'speakers' =&gt; [\n            'first_day' =&gt; ['rosa', 'judith'],\n        ],\n    ],\n    [\n        'name' =&gt; 'vueconf',\n        'speakers' =&gt; [\n            'first_day' =&gt; ['abigail', 'joey'],\n        ],\n    ],\n]);\n\n$plucked = $collection-&gt;pluck('speakers.first_day');\n\n$plucked-&gt;all();\n\/\/ output: [['rosa', 'judith'], ['abigail', 'joey']]\n<\/pre>\n<h3> \u5904\u7406\u91cd\u590d\u9879 <\/h3>\n<p>\u5904\u7406\u5177\u6709\u91cd\u590d\u952e\u7684\u96c6\u5408\u65f6\uff0cpluck() \u5c06\u4f7f\u7528\u4e0e\u6bcf\u4e2a\u952e\u5173\u8054\u7684\u6700\u540e\u4e00\u4e2a\u503c\uff1a<\/p>\n<pre>$collection = collect([\n    ['brand' =&gt; 'tesla',  'color' =&gt; 'red'],\n    ['brand' =&gt; 'pagani', 'color' =&gt; 'white'],\n    ['brand' =&gt; 'tesla',  'color' =&gt; 'black'],\n    ['brand' =&gt; 'pagani', 'color' =&gt; 'orange'],\n]);\n\n$plucked = $collection-&gt;pluck('color', 'brand');\n\n$plucked-&gt;all();\n\/\/ output: ['tesla' =&gt; 'black', 'pagani' =&gt; 'orange']\n<\/pre>\n<h2> \u4ec0\u4e48\u662f\u9009\u62e9\uff08\uff09\uff1f <\/h2>\n<p>laravel \u4e2d\u7684 select() \u65b9\u6cd5\u66f4\u7c7b\u4f3c\u4e8e sql \u7684 select \u8bed\u53e5\uff0c\u5141\u8bb8\u60a8\u4ece\u96c6\u5408\u4e2d\u9009\u62e9\u591a\u4e2a\u952e\uff0c\u5e76\u4ec5\u8fd4\u56de\u8fd9\u4e9b\u952e\u4f5c\u4e3a\u65b0\u96c6\u5408\u3002<\/p>\n<h3> select() \u7684\u793a\u4f8b <\/h3>\n<p>\u8ba9\u6211\u4eec\u8003\u8651\u4e00\u4e2a\u60a8\u60f3\u8981\u68c0\u7d22\u540d\u79f0\u548c\u89d2\u8272\u7684\u7528\u6237\u96c6\u5408\uff1a<\/p>\n<pre>$users = collect([\n    ['name' =&gt; 'Taylor Otwell', 'role' =&gt; 'Developer', 'status' =&gt; 'active'],\n    ['name' =&gt; 'Victoria Faith', 'role' =&gt; 'Researcher', 'status' =&gt; 'active'],\n]);\n\n$selectedUsers = $users-&gt;select(['name', 'role']);\n\n$selectedUsers-&gt;all();\n\/\/ Output: [\n\/\/     ['name' =&gt; 'Taylor Otwell', 'role' =&gt; 'Developer'],\n\/\/     ['name' =&gt; 'Victoria Faith', 'role' =&gt; 'Researcher'],\n\/\/ ]\n<\/pre>\n<p>\u4f7f\u7528 select()\uff0c\u53ef\u4ee5\u4e00\u6b21\u6027\u4ece\u96c6\u5408\u4e2d\u62c9\u53d6\u591a\u4e2a\u5c5e\u6027\u3002<\/p>\n<h2> pluck() \u548c select() \u4e4b\u95f4\u7684\u4e3b\u8981\u533a\u522b <\/h2>\n<ul>\n<li>\n<p><strong>\u76ee\u7684<\/strong>\uff1a<\/p>\n<ul>\n<li> pluck() \u7528\u4e8e\u4ece\u96c6\u5408\u4e2d\u63d0\u53d6\u5355\u4e2a\u5c5e\u6027\u6216\u952e\u503c\u5bf9\u3002<\/li>\n<li> select() \u7528\u4e8e\u4ece\u96c6\u5408\u4e2d\u7684\u6bcf\u4e2a\u9879\u76ee\u4e2d\u68c0\u7d22\u591a\u4e2a\u5c5e\u6027\uff0c\u7c7b\u4f3c\u4e8e sql \u67e5\u8be2\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8fd4\u56de\u7ed3\u6784<\/strong>:<\/p>\n<ul>\n<li> \u5f53\u63d0\u4f9b\u7b2c\u4e8c\u4e2a\u952e\u65f6\uff0cpluck() \u8fd4\u56de\u503c\u7684\u5e73\u9762\u6570\u7ec4\u6216\u5173\u8054\u6570\u7ec4\u3002<\/li>\n<li> select() \u8fd4\u56de\u4ec5\u5305\u542b\u6307\u5b9a\u952e\u7684\u6570\u7ec4\u96c6\u5408\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7528\u6cd5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5f53\u60a8\u9700\u8981\u7279\u5b9a\u952e\u7684\u503c\u5217\u8868\u65f6\uff0c\u8bf7\u4f7f\u7528 pluck()\u3002<\/li>\n<li>\u5f53\u60a8\u9700\u8981\u96c6\u5408\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u591a\u4e2a\u5b57\u6bb5\u65f6\uff0c\u8bf7\u4f7f\u7528 select()\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2> \u4f55\u65f6\u4f7f\u7528\u54ea\u4e2a\uff1f <\/h2>\n<ul>\n<li>\n<p><strong>\u5f53<\/strong>:<\/p>\n<p>\u65f6\u4f7f\u7528 pluck() <\/p>\n<ul>\n<li>\u60a8\u9700\u8981\u4ece\u5355\u4e2a\u952e\u4e2d\u63d0\u53d6\u503c\u3002<\/li>\n<li>\u60a8\u6b63\u5728\u5904\u7406\u5d4c\u5957\u6570\u636e\u5e76\u60f3\u8981\u68c0\u7d22\u7279\u5b9a\u7684\u5d4c\u5957\u5c5e\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5f53<\/strong>:<\/p>\n<p>\u65f6\u4f7f\u7528 select() <\/p>\n<ul>\n<li>\u60a8\u9700\u8981\u68c0\u7d22\u591a\u4e2a\u952e\u6216\u5c5e\u6027\u3002<\/li>\n<li>\u60a8\u60f3\u8981\u91cd\u7ec4\u6570\u636e\u4ee5\u4e13\u6ce8\u4e8e\u67d0\u4e9b\u5b57\u6bb5\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2> \u7ed3\u8bba <\/h2>\n<p>\u5728 laravel 11 \u4e2d\uff0cpluck() \u548c select() \u90fd\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u65b9\u6cd5\u6765\u64cd\u4f5c\u96c6\u5408\u3002 pluck() \u7b80\u5316\u4e86\u63d0\u53d6\u5355\u4e2a\u5c5e\u6027\u7684\u8fc7\u7a0b\uff0c\u800c select() \u5728\u9700\u8981\u5904\u7406\u591a\u4e2a\u5c5e\u6027\u65f6\u4e3a\u60a8\u63d0\u4f9b\u4e86\u66f4\u591a\u63a7\u5236\u6743\u3002\u4e86\u89e3\u8fd9\u4e24\u79cd\u65b9\u6cd5\u4e4b\u95f4\u7684\u5dee\u5f02\u53ef\u4ee5\u8ba9\u60a8\u4f18\u5316\u6570\u636e\u64cd\u4f5c\u6d41\u7a0b\u5e76\u7f16\u5199\u66f4\u6e05\u6670\u3001\u66f4\u9ad8\u6548\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u901a\u8fc7\u638c\u63e1 pluck() \u548c select()\uff0c\u60a8\u53ef\u4ee5\u5728 laravel \u5e94\u7528\u7a0b\u5e8f\u4e2d\u8f7b\u677e\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002\u5feb\u4e50\u7f16\u7801\uff01<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u7406\u89e3 Laravel 11 \u4e2d pluck() \u548c select() \u4e4b\u95f4\u7684\u533a\u522b\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7406\u89e3 Laravel 11 \u4e2d pluck() \u548c select() \u4e4b\u95f4\u7684\u533a\u522b \u6536\u85cf \u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300a\u7406\u89e3 Laravel 11 \u4e2d pluck() \u548c select() \u4e4b\u95f4\u7684\u533a\u522b\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 laravel \u662f\u6700\u6d41\u884c\u7684 php \u6846\u67b6\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\u3002\u5176\u4e2d\uff0cpluck() \u548c select() \u5728\u5904\u7406\u96c6\u5408\u65f6\u7ecf\u5e38\u4f7f\u7528\u3002\u5c3d\u7ba1\u5b83\u4eec\u770b\u8d77\u6765\u76f8\u4f3c\uff0c\u4f46\u5b83\u4eec\u7684\u76ee\u7684\u4e0d\u540c\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u8fd9\u4e24\u79cd\u65b9\u6cd5\u4e4b\u95f4\u7684\u5dee\u5f02\uff0c\u89e3\u91ca\u4f55\u65f6\u4f7f\u7528\u6bcf\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5b9e\u9645\u7684\u7f16\u7801\u793a\u4f8b\u6765\u6f14\u793a\u5b83\u4eec\u5728 laravel 11 \u4e2d\u7684\u7528\u6cd5\u3002 \u4ec0\u4e48\u662f pluck()\uff1f pluck() \u65b9\u6cd5\u65e8\u5728\u4ece\u96c6\u5408\u4e2d\u7684\u5355\u4e2a\u952e\u4e2d\u63d0\u53d6\u503c\u3002\u5f53\u60a8\u60f3\u8981\u4ece\u6570\u7ec4\u6216\u5bf9\u8c61\u96c6\u5408\u4e2d\u68c0\u7d22\u7279\u5b9a\u5c5e\u6027\u65f6\uff0c\u5b83\u7279\u522b\u65b9\u4fbf\u3002 pluck() \u7684\u793a\u4f8b \u5047\u8bbe\u60a8\u6709\u4e00\u7cfb\u5217\u4ea7\u54c1\uff0c\u5e76\u4e14\u60a8\u53ea\u60f3\u63d0\u53d6\u4ea7\u54c1\u540d\u79f0\uff1a $collection = collect([ [&#8216;product_id&#8217; =&gt; &#8216;prod-100&#8217;, &#8216;name&#8217; =&gt; &#8216;desk&#8217;], [&#8216;product_id&#8217; =&gt; &#8216;prod-200&#8217;, &#8216;name&#8217; =&gt; &#8216;chair&#8217;], ]); \/\/ pluck only the names [&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-48506","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48506","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=48506"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48506\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}