{"id":180,"date":"2023-02-19T19:21:38","date_gmt":"2023-02-19T11:21:38","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=180"},"modified":"2023-02-19T19:21:49","modified_gmt":"2023-02-19T11:21:49","slug":"laravel-count-usage","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/19\/laravel-count-usage\/","title":{"rendered":"Laravel count\u7684\u4f7f\u7528"},"content":{"rendered":"<h2>\u805a\u5408\u65b9\u6cd5<\/h2>\n<p>Laravel\u67e5\u8be2\u6784\u5efa\u5668\u63d0\u4f9b\u4e86\u5404\u79cd\u805a\u5408\u65b9\u6cd5<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-php\">$users = DB::table(&#039;users&#039;)-&gt;count();\n$price = DB::table(&#039;orders&#039;)-&gt;max(&#039;price&#039;);\n$price = DB::table(&#039;orders&#039;)-&gt;min(&#039;price&#039;);\n$price = DB::table(&#039;orders&#039;)-&gt;avg(&#039;price&#039;);\n$total = DB::table(&#039;users&#039;)-&gt;sum(&#039;votes&#039;);<\/code><\/pre>\n<h2>Raw Expressions<\/h2>\n<p>\u6709\u65f6\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u539f\u59cb\u8868\u8fbe\u5f0f\u7684\u67e5\u8be2\uff0c\u53ef\u4ee5\u4f7f\u7528<code>DB:raw<\/code>\u65b9\u6cd5<\/p>\n<pre><code class=\"language-php\">$patch_stat = DB::table(&#039;app_version_patchs&#039;)\n    -&gt;select(DB::raw(&#039;count(*) as patch_count&#039;))\n    -&gt;where(&#039;curr_version_id&#039;, $latest_version-&gt;id)-&gt;get()-&gt;first();\n\n$user_stat = DB::table(&#039;users&#039;)\n    -&gt;select(DB::raw(&#039;count(*) as user_count, status&#039;))\n    -&gt;where(&#039;status&#039;, &#039;&lt;&gt;&#039;, 1)\n    -&gt;groupBy(&#039;status&#039;)\n    -&gt;get()\n    -&gt;first();<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u805a\u5408\u65b9\u6cd5 Laravel\u67e5\u8be2\u6784\u5efa\u5668\u63d0\u4f9b\u4e86\u5404\u79cd\u805a\u5408\u65b9\u6cd5 $users = DB::table(&#039;use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-180","post","type-post","status-publish","format-standard","hentry","category-laravel"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=180"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/180\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}