第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel - 使用數(shù)據(jù)庫功能改進的數(shù)據(jù)填充圖表

Laravel - 使用數(shù)據(jù)庫功能改進的數(shù)據(jù)填充圖表

PHP
動漫人物 2023-11-04 19:58:40
我有一個管理儀表板,顯示折線圖,其中包含每月注冊的用戶數(shù)量。我正在從數(shù)據(jù)庫獲取數(shù)據(jù),似乎一切正常,但功能相當大,我想知道是否有更優(yōu)化和更有效的方法來獲取和返回相同的數(shù)據(jù)?我將此函數(shù)分配給一個變量并將其傳遞到我的視圖。// Get Monthly Registered Users This yearpublic function monthlyRegisteredUsers(){    $janUsers = User::whereMonth('created_at', 1)->whereYear('created_at', Carbon::now()->format('Y'))->count();    $febUsers = User::whereMonth('created_at', 2)->whereYear('created_at', Carbon::now()->format('Y'))->count();    $marUsers = User::whereMonth('created_at', 3)->whereYear('created_at', Carbon::now()->format('Y'))->count();    $aprUsers = User::whereMonth('created_at', 4)->whereYear('created_at', Carbon::now()->format('Y'))->count();    $mayUsers = User::whereMonth('created_at', 5)->whereYear('created_at', Carbon::now()->format('Y'))->count();    $junUsers = User::whereMonth('created_at', 6)->whereYear('created_at', Carbon::now()->format('Y'))->count();    $julUsers = User::whereMonth('created_at', 7)->whereYear('created_at', Carbon::now()->format('Y'))->count();    $augUsers = User::whereMonth('created_at', 8)->whereYear('created_at', Carbon::now()->format('Y'))->count();    $sepUsers = User::whereMonth('created_at', 9)->whereYear('created_at', Carbon::now()->format('Y'))->count();    $octUsers = User::whereMonth('created_at', 10)->whereYear('created_at', Carbon::now()->format('Y'))->count();    $novUsers = User::whereMonth('created_at', 11)->whereYear('created_at', Carbon::now()->format('Y'))->count();    $decUsers = User::whereMonth('created_at', 12)->whereYear('created_at', Carbon::now()->format('Y'))->count();    $data  = [$janUsers, $febUsers, $marUsers, $aprUsers, $mayUsers, $junUsers, $julUsers, $augUsers, $sepUsers, $octUsers, $novUsers, $decUsers ];    return $data;}它所做的就是獲取每個月的注冊用戶數(shù)并將其分配給一個變量,然后返回包含每月用戶數(shù)的數(shù)組。這可以改進嗎?
查看完整描述

1 回答

?
人到中年有點甜

TA貢獻1895條經(jīng)驗 獲得超7個贊

您可以使用 array_map 和 range 來使此代碼更短:


public function monthlyRegisteredUsers()

{

  return array_map(function($month){

    return User::whereMonth('created_at', $month)->whereYear('created_at', Carbon::now()->format('Y'))->count();

  }, range(1,12))

}

我認為您可以使用 group by 來運行一個查詢,讓我通過更好的查詢來更新我的答案。


public function monthlyRegisteredUsers()

{

  $counts = User::select(DB::raw('MONTH(created_at) month, count(*) as count'))

            ->whereYear('created_at', Carbon::now()->format('Y'))

            ->groupBy(DB::raw('MONTH(created_at)'))

            ->pluck('count', 'month')

            ->toArray();

   return array_map(function($month) use ($counts){

            return Arr::get($counts, $month, 0);

        }, range(1,12));

}


查看完整回答
反對 回復 2023-11-04
  • 1 回答
  • 0 關注
  • 126 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號