{"id":22800,"date":"2024-11-21T12:33:45","date_gmt":"2024-11-21T04:33:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22800\/"},"modified":"2024-11-21T12:33:45","modified_gmt":"2024-11-21T04:33:45","slug":"laravel%e6%b1%82%e6%9f%90%e4%b8%80%e5%88%97sum","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22800\/","title":{"rendered":"laravel\u6c42\u67d0\u4e00\u5217sum"},"content":{"rendered":"<p>laravel\u662f\u76ee\u524d\u6700\u6d41\u884c\u7684php\u6846\u67b6\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u4fbf\u6377\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u5f00\u53d1\u4eba\u5458\u66f4\u52a0\u9ad8\u6548\u5730\u5b8c\u6210\u4efb\u52a1\u3002\u5176\u4e2d\u4e00\u4e2a\u5e38\u7528\u7684\u529f\u80fd\u662f\u5bf9\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528laravel\u5bf9\u67d0\u4e00\u5217\u8fdb\u884c\u6c42\u548c\u3002<\/p>\n<p>\u5728Laravel\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Eloquent\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002Eloquent\u662fLaravel\u7684ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u6a21\u5757\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5e93\u8868\u683c\u6620\u5c04\u4e3a\u5bf9\u8c61\u6a21\u578b\uff0c\u65b9\u4fbf\u6211\u4eec\u8fdb\u884c\u64cd\u4f5c\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728Laravel\u4e2d\u5b9a\u4e49\u4e00\u4e2aEloquent\u6a21\u578b\uff0c\u7528\u4e8e\u6620\u5c04\u6211\u4eec\u8981\u64cd\u4f5c\u7684\u8868\u683c\u3002\u5047\u8bbe\u6211\u4eec\u8981\u5bf9\u8868\u683c\u4e2d\u7684\u67d0\u4e00\u5217\u201camount\u201d\u8fdb\u884c\u6c42\u548c\uff0c\u5b9a\u4e49\u6a21\u578b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>namespace App;\n\nuse IlluminateDatabaseEloquentModel;\n\nclass Transaction extends Model\n{\n    protected $table = 'transactions';\n    protected $fillable = ['amount', 'description'];\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a\u201cTransaction\u201d\u7684\u6a21\u578b\uff0c\u5b83\u5bf9\u5e94\u7684\u6570\u636e\u5e93\u8868\u683c\u540d\u4e3a\u201ctransactions\u201d\u3002\u6211\u4eec\u5728fillable\u5c5e\u6027\u4e2d\u5b9a\u4e49\u4e86\u8868\u683c\u4e2d\u53ef\u88ab\u6279\u91cf\u8d4b\u503c\u7684\u5b57\u6bb5\uff0c\u8fd9\u91cc\u5305\u62ec\u201camount\u201d\u548c\u201cdescription\u201d\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Eloquent\u63d0\u4f9b\u7684\u65b9\u6cd5\u5bf9\u201camount\u201d\u5217\u8fdb\u884c\u6c42\u548c\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u63a7\u5236\u5668\u4e2d\u7f16\u5199\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>namespace AppHttpControllers;\n\nuse AppTransaction;\nuse IlluminateHttpRequest;\n\nclass TransactionController extends Controller\n{\n    public function totalAmount()\n    {\n        $sum = Transaction::sum('amount');\n        return view('transactions.total_amount', compact('sum'));\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Laravel\u7684\u4f9d\u8d56\u6ce8\u5165\u529f\u80fd\u5f15\u5165\u4e86Request\u7c7b\u548cTransaction\u6a21\u578b\u3002\u7136\u540e\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u540d\u4e3a\u201ctotalAmount\u201d\u7684\u65b9\u6cd5\uff0c\u5b83\u901a\u8fc7\u8c03\u7528Transaction\u6a21\u578b\u7684sum\u65b9\u6cd5\u6765\u5bf9\u201camount\u201d\u5217\u8fdb\u884c\u6c42\u548c\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c06\u6c42\u548c\u7ed3\u679c\u4f20\u9012\u7ed9\u89c6\u56fe\u6587\u4ef6\u201ctransactions.total_amount\u201d\u8fdb\u884c\u5c55\u793a\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u5728\u8def\u7531\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u8def\u7531\uff0c\u7528\u4e8e\u8bbf\u95ee\u8fd9\u4e2a\u65b9\u6cd5\u3002\u5047\u8bbe\u6211\u4eec\u8981\u5c06\u8be5\u65b9\u6cd5\u7ed1\u5b9a\u5230\u8def\u7531\u201c\/transactions\/total-amount\u201d\uff0c\u5219\u53ef\u4ee5\u5728routes\/web.php\u4e2d\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>Route::get('\/transactions\/total-amount', 'TransactionController@totalAmount');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee\u7f51\u5740\u201chttp:\/\/yourdomain.com\/transactions\/total-amount\u201d\uff0c\u5373\u53ef\u67e5\u770b\u201camount\u201d\u5217\u7684\u603b\u548c\u3002<\/p>\n<p>\u603b\u7ed3\u4e00\u4e0b\uff0cLaravel\u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u5bf9\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u3002\u6211\u4eec\u53ea\u9700\u8981\u4f7f\u7528Eloquent\u6a21\u578b\u7684sum\u65b9\u6cd5\u5373\u53ef\u8f7b\u677e\u5730\u5b9e\u73b0\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel\u6c42\u67d0\u4e00\u5217sum\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\u76ee\u524d\u6700\u6d41\u884c\u7684php\u6846\u67b6\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u4fbf\u6377\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u5f00\u53d1\u4eba\u5458\u66f4\u52a0\u9ad8\u6548\u5730\u5b8c\u6210\u4efb\u52a1\u3002\u5176\u4e2d\u4e00\u4e2a\u5e38\u7528\u7684\u529f\u80fd\u662f\u5bf9\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528laravel\u5bf9\u67d0\u4e00\u5217\u8fdb\u884c\u6c42\u548c\u3002 \u5728Laravel\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Eloquent\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002Eloquent\u662fLaravel\u7684ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u6a21\u5757\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5e93\u8868\u683c\u6620\u5c04\u4e3a\u5bf9\u8c61\u6a21\u578b\uff0c\u65b9\u4fbf\u6211\u4eec\u8fdb\u884c\u64cd\u4f5c\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728Laravel\u4e2d\u5b9a\u4e49\u4e00\u4e2aEloquent\u6a21\u578b\uff0c\u7528\u4e8e\u6620\u5c04\u6211\u4eec\u8981\u64cd\u4f5c\u7684\u8868\u683c\u3002\u5047\u8bbe\u6211\u4eec\u8981\u5bf9\u8868\u683c\u4e2d\u7684\u67d0\u4e00\u5217\u201camount\u201d\u8fdb\u884c\u6c42\u548c\uff0c\u5b9a\u4e49\u6a21\u578b\u4ee3\u7801\u5982\u4e0b\uff1a namespace App; use IlluminateDatabaseEloquentModel; class Transaction extends Model { protected $table = &#8216;transactions&#8217;; protected $fillable = [&#8216;amount&#8217;, &#8216;description&#8217;]; } \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a\u201cTransaction\u201d\u7684\u6a21\u578b\uff0c\u5b83\u5bf9\u5e94\u7684\u6570\u636e\u5e93\u8868\u683c\u540d\u4e3a\u201ctransactions\u201d\u3002\u6211\u4eec\u5728fillable\u5c5e\u6027\u4e2d\u5b9a\u4e49\u4e86\u8868\u683c\u4e2d\u53ef\u88ab\u6279\u91cf\u8d4b\u503c\u7684\u5b57\u6bb5\uff0c\u8fd9\u91cc\u5305\u62ec\u201camount\u201d\u548c\u201cdescription\u201d\u3002 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Eloquent\u63d0\u4f9b\u7684\u65b9\u6cd5\u5bf9\u201camount\u201d\u5217\u8fdb\u884c\u6c42\u548c\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u63a7\u5236\u5668\u4e2d\u7f16\u5199\u5982\u4e0b\u4ee3\u7801\uff1a namespace AppHttpControllers; use AppTransaction; use IlluminateHttpRequest; class TransactionController extends Controller { public function totalAmount() { $sum = Transaction::sum(&#8216;amount&#8217;); return view(&#8216;transactions.total_amount&#8217;, compact(&#8216;sum&#8217;)); } } \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Laravel\u7684\u4f9d\u8d56\u6ce8\u5165\u529f\u80fd\u5f15\u5165\u4e86Request\u7c7b\u548cTransaction\u6a21\u578b\u3002\u7136\u540e\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u540d\u4e3a\u201ctotalAmount\u201d\u7684\u65b9\u6cd5\uff0c\u5b83\u901a\u8fc7\u8c03\u7528Transaction\u6a21\u578b\u7684sum\u65b9\u6cd5\u6765\u5bf9\u201camount\u201d\u5217\u8fdb\u884c\u6c42\u548c\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c06\u6c42\u548c\u7ed3\u679c\u4f20\u9012\u7ed9\u89c6\u56fe\u6587\u4ef6\u201ctransactions.total_amount\u201d\u8fdb\u884c\u5c55\u793a\u3002 \u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u5728\u8def\u7531\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u8def\u7531\uff0c\u7528\u4e8e\u8bbf\u95ee\u8fd9\u4e2a\u65b9\u6cd5\u3002\u5047\u8bbe\u6211\u4eec\u8981\u5c06\u8be5\u65b9\u6cd5\u7ed1\u5b9a\u5230\u8def\u7531\u201c\/transactions\/total-amount\u201d\uff0c\u5219\u53ef\u4ee5\u5728routes\/web.php\u4e2d\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a Route::get(&#8216;\/transactions\/total-amount&#8217;, &#8216;TransactionController@totalAmount&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u73b0\u5728\uff0c\u6211\u4eec\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee\u7f51\u5740\u201chttp:\/\/yourdomain.com\/transactions\/total-amount\u201d\uff0c\u5373\u53ef\u67e5\u770b\u201camount\u201d\u5217\u7684\u603b\u548c\u3002 \u603b\u7ed3\u4e00\u4e0b\uff0cLaravel\u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u5bf9\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u3002\u6211\u4eec\u53ea\u9700\u8981\u4f7f\u7528Eloquent\u6a21\u578b\u7684sum\u65b9\u6cd5\u5373\u53ef\u8f7b\u677e\u5730\u5b9e\u73b0\u3002 [&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-22800","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22800","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=22800"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22800\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}