{"id":22805,"date":"2024-11-21T12:03:01","date_gmt":"2024-11-21T04:03:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22805\/"},"modified":"2024-11-21T12:03:01","modified_gmt":"2024-11-21T04:03:01","slug":"laravel-sum-%e4%b8%8d%e5%90%8c%e6%9d%a1%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22805\/","title":{"rendered":"laravel sum \u4e0d\u540c\u6761\u4ef6"},"content":{"rendered":"<p>laravel\u662f\u4e00\u79cd\u6d41\u884c\u7684php\u5f00\u53d1\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u51fd\u6570\u548c\u8bed\u6cd5\u6765\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002\u5176\u4e2d\uff0csum\u51fd\u6570\u662f\u5728laravel\u4e2d\u975e\u5e38\u5e38\u7528\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u96c6\u5408\u4e2d\u7684\u503c\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u3002\u4f46\u662f\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u6709\u65f6\u9700\u8981\u5728\u4e0d\u540c\u7684\u6761\u4ef6\u4e0b\u8fdb\u884c\u6c42\u548c\uff0c\u8fd9\u65f6\u8be5\u5982\u4f55\u5e94\u5bf9\u5462\uff1f\u672c\u6587\u5c06\u4ecb\u7ecdlaravel\u5982\u4f55\u5728\u4e0d\u540c\u6761\u4ef6\u4e0b\u8fdb\u884csum\u6c42\u548c\u64cd\u4f5c\u3002<\/p>\n<p>\u4e00\u3001\u7b80\u5355\u60c5\u51b5\u4e0b\u7684sum\u64cd\u4f5c<\/p>\n<p>\u5728Laravel\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5bf9\u4e00\u4e2a\u96c6\u5408\u8fdb\u884csum\u64cd\u4f5c\uff0c\u53ea\u9700\u8c03\u7528sum\u51fd\u6570\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u5bf9\u4e00\u4e2a\u6570\u7ec4\u8fdb\u884c\u6c42\u548c\uff1a<\/p>\n<pre>$sum = collect([1, 2, 3, 4, 5])-&amp;gt;sum();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c$sum\u7684\u503c\u4e3a15\uff0c\u53731+2+3+4+5\u7684\u548c\u3002<\/p>\n<p>\u7c7b\u4f3c\u5730\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u5bf9\u8c61\u6570\u7ec4\uff0c\u60f3\u8981\u5bf9\u5176\u4e2d\u7684\u67d0\u4e2a\u5c5e\u6027\u8fdb\u884c\u6c42\u548c\uff0c\u53ef\u4ee5\u8fd9\u6837\u64cd\u4f5c\uff1a<\/p>\n<pre>$sum = collect([\n    ['name' =&amp;gt; 'Alice', 'score' =&amp;gt; 80],\n    ['name' =&amp;gt; 'Bob', 'score' =&amp;gt; 90],\n    ['name' =&amp;gt; 'Charlie', 'score' =&amp;gt; 70]\n])-&amp;gt;sum('score');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c$sum\u7684\u503c\u4e3a240\uff0c\u537380+90+70\u7684\u548c\u3002<\/p>\n<p>\u4e8c\u3001\u6761\u4ef6\u6c42\u548c\u64cd\u4f5c<\/p>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5728\u4e0d\u540c\u7684\u6761\u4ef6\u4e0b\u8fdb\u884csum\u64cd\u4f5c\uff0c\u8fd9\u65f6\u5c31\u9700\u8981\u7528\u5230Laravel\u4e2d\u7684where\u51fd\u6570\u3002where\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u8fc7\u6ee4\u96c6\u5408\u4e2d\u7684\u503c\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u7528\u6237\u6570\u7ec4\uff0c\u6bcf\u4e2a\u7528\u6237\u90fd\u6709\u59d3\u540d\u548c\u5e74\u9f84\u4e24\u4e2a\u5c5e\u6027\uff0c\u60f3\u8981\u8ba1\u7b97\u6240\u6709\u5e74\u9f84\u5927\u4e8e18\u5c81\u7684\u7528\u6237\u5e74\u9f84\u4e4b\u548c\uff0c\u53ef\u4ee5\u8fd9\u6837\u64cd\u4f5c\uff1a<\/p>\n<pre>$users = collect([\n    ['name' =&amp;gt; 'Alice', 'age' =&amp;gt; 20],\n    ['name' =&amp;gt; 'Bob', 'age' =&amp;gt; 16],\n    ['name' =&amp;gt; 'Charlie', 'age' =&amp;gt; 25]\n]);\n\n$sum = $user-&amp;gt;where('age', '&amp;gt;', 18)-&amp;gt;sum('age');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c$sum\u7684\u503c\u4e3a45\uff0c\u537320+25\u7684\u548c\u3002<\/p>\n<p>\u7c7b\u4f3c\u5730\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u8ba2\u5355\u6570\u7ec4\uff0c\u6bcf\u4e2a\u8ba2\u5355\u90fd\u6709\u4e00\u4e2a\u72b6\u6001\u5c5e\u6027\uff0c\u60f3\u8981\u8ba1\u7b97\u6240\u6709\u5df2\u5b8c\u6210\u8ba2\u5355\u7684\u91d1\u989d\u4e4b\u548c\uff0c\u53ef\u4ee5\u8fd9\u6837\u64cd\u4f5c\uff1a<\/p>\n<pre>$orders = collect([\n    ['id' =&amp;gt; 1, 'status' =&amp;gt; 'created', 'amount' =&amp;gt; 10],\n    ['id' =&amp;gt; 2, 'status' =&amp;gt; 'completed', 'amount' =&amp;gt; 20],\n    ['id' =&amp;gt; 3, 'status' =&amp;gt; 'cancelled', 'amount' =&amp;gt; 30],\n    ['id' =&amp;gt; 4, 'status' =&amp;gt; 'completed', 'amount' =&amp;gt; 40]\n]);\n\n$sum = $orders-&amp;gt;where('status', 'completed')-&amp;gt;sum('amount');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c$sum\u7684\u503c\u4e3a60\uff0c\u537320+40\u7684\u548c\u3002<\/p>\n<p>\u4e09\u3001\u6839\u636e\u591a\u4e2a\u6761\u4ef6\u6c42\u548c<\/p>\n<p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u591a\u4e2a\u6761\u4ef6\u8fdb\u884csum\u64cd\u4f5c\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528Laravel\u4e2d\u7684where\u51fd\u6570\u7684\u94fe\u5f0f\u8c03\u7528\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u7528\u6237\u6570\u7ec4\uff0c\u6bcf\u4e2a\u7528\u6237\u90fd\u6709\u59d3\u540d\u3001\u5e74\u9f84\u3001\u5730\u533a\u4e09\u4e2a\u5c5e\u6027\uff0c\u60f3\u8981\u8ba1\u7b97\u6240\u6709\u5730\u533a\u4e3a\u5317\u4eac\u3001\u5e74\u9f84\u5927\u4e8e18\u5c81\u7684\u7528\u6237\u5e74\u9f84\u4e4b\u548c\uff0c\u53ef\u4ee5\u8fd9\u6837\u64cd\u4f5c\uff1a<\/p>\n<pre>$users = collect([\n    ['name' =&amp;gt; 'Alice', 'age' =&amp;gt; 20, 'area' =&amp;gt; 'Beijing'],\n    ['name' =&amp;gt; 'Bob', 'age' =&amp;gt; 16, 'area' =&amp;gt; 'Shanghai'],\n    ['name' =&amp;gt; 'Charlie', 'age' =&amp;gt; 25, 'area' =&amp;gt; 'Beijing']\n]);\n\n$sum = $user-&amp;gt;where('age', '&amp;gt;', 18)-&amp;gt;where('area', 'Beijing')-&amp;gt;sum('age');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c$sum\u7684\u503c\u4e3a20\uff0c\u5373Alice\u7684\u5e74\u9f84\u3002<\/p>\n<p>\u7c7b\u4f3c\u5730\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u8ba2\u5355\u6570\u7ec4\uff0c\u6bcf\u4e2a\u8ba2\u5355\u90fd\u6709\u4e00\u4e2a\u72b6\u6001\u5c5e\u6027\u548c\u4e00\u4e2a\u65e5\u671f\u5c5e\u6027\uff0c\u60f3\u8981\u8ba1\u7b97\u6240\u6709\u5df2\u5b8c\u6210\u8ba2\u5355\u4e2d2019\u5e741\u67081\u65e5\u4e4b\u540e\u7684\u8ba2\u5355\u91d1\u989d\u4e4b\u548c\uff0c\u53ef\u4ee5\u8fd9\u6837\u64cd\u4f5c\uff1a<\/p>\n<pre>$orders = collect([\n    ['id' =&amp;gt; 1, 'status' =&amp;gt; 'created', 'amount' =&amp;gt; 10, 'date' =&amp;gt; '2019-01-01'],\n    ['id' =&amp;gt; 2, 'status' =&amp;gt; 'completed', 'amount' =&amp;gt; 20, 'date' =&amp;gt; '2019-01-05'],\n    ['id' =&amp;gt; 3, 'status' =&amp;gt; 'cancelled', 'amount' =&amp;gt; 30, 'date' =&amp;gt; '2018-12-31'],\n    ['id' =&amp;gt; 4, 'status' =&amp;gt; 'completed', 'amount' =&amp;gt; 40, 'date' =&amp;gt; '2019-02-01']\n]);\n\n$sum = $orders-&amp;gt;where('status', 'completed')-&amp;gt;where('date', '&amp;gt;', '2019-01-01')-&amp;gt;sum('amount');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c$sum\u7684\u503c\u4e3a20\uff0c\u5373\u7b2c\u4e8c\u4e2a\u8ba2\u5355\u7684\u91d1\u989d\u3002<\/p>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0cLaravel\u7684sum\u51fd\u6570\u548cwhere\u51fd\u6570\u975e\u5e38\u65b9\u4fbf\u5b9e\u7528\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u5730\u5b8c\u6210\u6761\u4ef6\u6c42\u548c\u64cd\u4f5c\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel sum \u4e0d\u540c\u6761\u4ef6\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\u79cd\u6d41\u884c\u7684php\u5f00\u53d1\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u51fd\u6570\u548c\u8bed\u6cd5\u6765\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002\u5176\u4e2d\uff0csum\u51fd\u6570\u662f\u5728laravel\u4e2d\u975e\u5e38\u5e38\u7528\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u96c6\u5408\u4e2d\u7684\u503c\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u3002\u4f46\u662f\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u6709\u65f6\u9700\u8981\u5728\u4e0d\u540c\u7684\u6761\u4ef6\u4e0b\u8fdb\u884c\u6c42\u548c\uff0c\u8fd9\u65f6\u8be5\u5982\u4f55\u5e94\u5bf9\u5462\uff1f\u672c\u6587\u5c06\u4ecb\u7ecdlaravel\u5982\u4f55\u5728\u4e0d\u540c\u6761\u4ef6\u4e0b\u8fdb\u884csum\u6c42\u548c\u64cd\u4f5c\u3002 \u4e00\u3001\u7b80\u5355\u60c5\u51b5\u4e0b\u7684sum\u64cd\u4f5c \u5728Laravel\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5bf9\u4e00\u4e2a\u96c6\u5408\u8fdb\u884csum\u64cd\u4f5c\uff0c\u53ea\u9700\u8c03\u7528sum\u51fd\u6570\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u5bf9\u4e00\u4e2a\u6570\u7ec4\u8fdb\u884c\u6c42\u548c\uff1a $sum = collect([1, 2, 3, 4, 5])-&amp;gt;sum(); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c$sum\u7684\u503c\u4e3a15\uff0c\u53731+2+3+4+5\u7684\u548c\u3002 \u7c7b\u4f3c\u5730\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u5bf9\u8c61\u6570\u7ec4\uff0c\u60f3\u8981\u5bf9\u5176\u4e2d\u7684\u67d0\u4e2a\u5c5e\u6027\u8fdb\u884c\u6c42\u548c\uff0c\u53ef\u4ee5\u8fd9\u6837\u64cd\u4f5c\uff1a $sum = collect([ [&#8216;name&#8217; =&amp;gt; &#8216;Alice&#8217;, &#8216;score&#8217; =&amp;gt; 80], [&#8216;name&#8217; =&amp;gt; &#8216;Bob&#8217;, &#8216;score&#8217; =&amp;gt; 90], [&#8216;name&#8217; =&amp;gt; &#8216;Charlie&#8217;, &#8216;score&#8217; =&amp;gt; 70] ])-&amp;gt;sum(&#8216;score&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c$sum\u7684\u503c\u4e3a240\uff0c\u537380+90+70\u7684\u548c\u3002 \u4e8c\u3001\u6761\u4ef6\u6c42\u548c\u64cd\u4f5c \u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5728\u4e0d\u540c\u7684\u6761\u4ef6\u4e0b\u8fdb\u884csum\u64cd\u4f5c\uff0c\u8fd9\u65f6\u5c31\u9700\u8981\u7528\u5230Laravel\u4e2d\u7684where\u51fd\u6570\u3002where\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u8fc7\u6ee4\u96c6\u5408\u4e2d\u7684\u503c\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u7528\u6237\u6570\u7ec4\uff0c\u6bcf\u4e2a\u7528\u6237\u90fd\u6709\u59d3\u540d\u548c\u5e74\u9f84\u4e24\u4e2a\u5c5e\u6027\uff0c\u60f3\u8981\u8ba1\u7b97\u6240\u6709\u5e74\u9f84\u5927\u4e8e18\u5c81\u7684\u7528\u6237\u5e74\u9f84\u4e4b\u548c\uff0c\u53ef\u4ee5\u8fd9\u6837\u64cd\u4f5c\uff1a $users = collect([ [&#8216;name&#8217; =&amp;gt; &#8216;Alice&#8217;, &#8216;age&#8217; =&amp;gt; 20], [&#8216;name&#8217; =&amp;gt; &#8216;Bob&#8217;, &#8216;age&#8217; =&amp;gt; 16], [&#8216;name&#8217; [&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-22805","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22805","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=22805"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22805\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}