やりたいこと
・ユーザーごとにchecklist==1の場合とchacklist==0の場合をそれぞれ出力したい
名前 | 研修済 | 研修未完 |
---|---|---|
user1 | 3 | 2 |
user2 | 1 | 0 |
user3 | 1 | 2 |
現状
・ユーザの名前は出力できているけど、「研修済」「研修未完」がuser1の情報がuser2,user3にも反映されている
名前 | 研修済 | 研修未完 |
---|---|---|
user1 | 3 | 2 |
user2 | 3 | 2 |
user3 | 3 | 2 |
質問
※foreachで繰り返す中でuserテーブルの['id']とmemosテーブルの['user_id']が一致したうえでchecklist==1 or 0をカウントする処理??
処理の方法を探してもなかなか記事も見つからないのでterataillで質問をさせていただきました
コード
AuthController.php
php
public function userlist(){ $user = \Auth::user(); $userlist = User::paginate(10); $memolists = Memo::where('checklist','1')->where('status','1')->get(); $memoyet = Memo::where('checklist','0')->where('status','1')->get(); //論理削除 status==2は削除扱い return view('userlist',compact('user','userlist','memolists','memoyet')); }
userlsit.blade.php
php
@extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <table class="table"> <h1>ユーザ一覧</h1> <thead class = "table-dark"> <tr> <th scope="col">ユーザー名前</th> <th scope="col">研修済</th> <th scope="col">研修未完</th> </tr> </thead> <tbody> @foreach($userlist as $list) <tr> <td> <a href = "/userdetail/{{ $list['id'] }}" class="name" > {{ $list['name'] }} </a> </td> <th scope="row"> <?php $i=0; ?> @foreach($memolists as $memolist) <?php $i++; ?> @endforeach <?php echo $i; ?> </th> <th scope="row"> <?php $i=0; ?> @foreach($memoyet as $yet) <?php $i++; ?> @endforeach <?php echo $i; ?> </th> </tr> @endforeach </tbody> </table> </div> </div> </div> @endsection
テーブル定義情報
usersテーブル
id | name |
---|---|
1 | test1 |
2 | test2 |
2 | test3 |
memosテーブル
id | user_id | status | checklist |
---|---|---|---|
1 | 1 | 1 | 0 |
2 | 1 | 1 | 0 |
3 | 1 | 1 | 1 |
4 | 1 | 1 | 1 |
5 | 1 | 1 | 1 |
6 | 2 | 1 | 1 |
7 | 3 | 1 | 1 |
8 | 3 | 1 | 0 |
9 | 3 | 1 | 0 |
まだ回答がついていません
会員登録して回答してみよう