前提・実現したいこと
LaravelとPHPを勉強し始めて10日くらいの初心者です。
DBにアクセスしてその月のチーム内(チームは1つだけ)のユーザー毎のプレイ数をカウントして
viewのテーブルに送りたいと考えていますが、思っている結果にできないので相談させてください。
理想は下記の"補足"のようにしたいのですが、数値の引き出し方法や計算法がまだ理解不足でして
どうかご教示をお願いいたします。
該当のソースコード
Controller
1private function getMonthPlay(string $user_id, Team $team) 2{ 3 $date = Carbon::now(); 4 $plays = Play::select('accountId', DB::raw('count(created_at) as total')) 5 ->where('team_id', $team->id) 6 ->whereMonth('created_at', $date->month) 7 ->GROUPBY('accountId') 8 ->get(); 9 10 return $plays; 11} 12 public function playerMember(Request $request) 13 { 14 15 $user = Auth::user(); 16 $teams = $user->teams; 17 $team = $teams[0]; 18 $users = $team->users; 19 20 $team_id = $team->id; 21 $user_id = $user->id; 22 23 $month_plays = $this->getMonthPlay($user_id,$team); 24 return view('playerMember',compact('users','user','month_plays')); 25 } 26
view
1<table> 2<tr> 3<th>Id</th><th>play数</th> 4 @foreach ($users as $user) 5 <tr> 6 <td>{{$user->id}}</td> 7 <td>{{$month_plays}}</td> 8 9 </tr> 10 @endforeach 11 </table>
Id play数 3 [{"accountId":1,"total":10}] 1 [{"accountId":1,"total":10}] 2 [{"accountId":1,"total":10}]
補足情報
Id play数
3 1
1 4
2 5


回答1件
あなたの回答
tips
プレビュー