doubo4824 2018-07-22 05:12
浏览 82
已采纳

如何使用Laravel Fluent COUNT列并使用每个ID(用户)的WHERE条件?

i'm newbie to laravel. I want to count the number with where condition in column 'ket'. In this code, i've successfully applied to normal user (code 1), i want to applied the code 1 $total_tdkmsk $total_msk $total_telat into code 2. So this is what i've done.

Database Structure

Laravel Fluent Query

$id_alias = Auth::user()->id;
$tdkmsk = 'Tidak Masuk';
$msk = 'Masuk';
$total_tdkmsk = DB::table('presensi')
                    -> where('presensi.ket','=',$tdkmsk)
                    -> where('presensi.id_user','=',$id_alias)
                    -> count();
$total_msk = DB::table('presensi')
                    -> where('presensi.ket','=',$msk)
                    -> where('presensi.id_user','=',$id_alias)
                    -> count();
$total_telat = DB::table('presensi')
                    -> where('presensi.id_user','=',$id_alias)
                    -> sum('ketidakhadiran');

into this query

$items = DB::table('presensi')
            ->join('jabatan', 'presensi.id_user', '=', 'jabatan.id')
            ->join('users', 'presensi.id_user', '=', 'users.id')
            ->join('jadwal', 'presensi.jadwal', '=', 'jadwal.id')
            ->join('matakuliah', 'jadwal.id', '=', 'matakuliah.id')
            ->join('mahasiswakelas', 'jadwal.id_kelas', '=', 'mahasiswakelas.id_kelas')
            ->join('kelas', 'mahasiswakelas.id_kelas', '=', 'kelas.id')
            ->select('users.id AS id_users','users.name AS nama_users' ,'presensi.id AS id','jabatan.id AS id_jabatan','presensi.*', 'matakuliah.namamatakuliah','jabatan.nama_jabatan','kelas.nama_kelas','mahasiswakelas.tahunakademik',DB::raw('COUNT(*) as tidakada'))
            -> where('presensi.ket','=',$tdkmsk)
            ->orderby('presensi.id', 'DESC')
            ->groupby('presensi.id_user')
            ->get();

I get the result, but this is not what i'm expected. Any advice for this? thank you very much

  • 写回答

1条回答 默认 最新

  • dongzi0602 2018-07-22 05:26
    关注

    For achieving what you just described below, you can do the following-

    You should have a field in your database which specifies which user is having the administrator rights and which user is a normal user.

    1. check if the logged in user is the administrator.
    2. If he is the administrator, Get the data of all the users.

      if($checked){
        //fetch the data of all normal users.
      }
      

    In your controller

    $query = DB::table('users')->where([
      ['role', '=', 'admin'],  // you should have a role field in the database.
      ['user_id', '=', $id_alias],
    ])->get();
    
    if($query){
      $total_tdkmsk = DB::table('presensi')
                    -> where('presensi.ket','=',$tdkmsk)
                    -> count();
      $total_msk = DB::table('presensi')
                    -> where('presensi.ket','=',$msk)
                    -> count();
      $total_telat = DB::table('presensi')
                    -> where('presensi.id_user','=',$id_alias)
                    -> sum('ketidakhadiran');
    }
    

    Edit the table names according to your database design. Also I'm not sure of the code written inside the if statement here. So check and debug it by yourself.

    Also since you want to fetch the information of all the users, you should not pass user_id in the where clause because you are not having user id's of all the users stored in the variable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算