如何使用Laravel检查值的总计数

时间:2015-08-08 10:47:12

标签: laravel-4 count

我有一张桌子' student_attendence'

我想要这样

RollNo  StudentName  02 13  14  15  16  17  20  Total Presents  Total Absents
  28     Gokul        p  p   p  p   a   p   p       6                 1
  31     Goku         p p   p   a   p   p   p       6                 1
  32     Gok          p p   p   p   p   a   a       5                 2

我想要了解每个学生的现在(p)和缺席(a)的总数。

我的控制器代码是:

$attendence_tbl = WysAttendancename::where('cls_id',$id)->first();
$wys_attendence_table = $attendence_tbl->attendance_name;
$attendance = DB::table($wys_attendence_table)
              ->where('amonth',$amonth)
              ->where('ayear',$ayear) 
              ->groupBy('adate')
              ->get();
$stud_attend = DB::table($wys_attendence_table)
               ->where('amonth',$amonth)
               ->where('ayear',$ayear) 
               ->get();

和我的view.blade php是

 @foreach($students as $student)                  
                  @if($student->studcls == $id)
                  <tr> 
                    <td>{{$student->id}}</td>
                    <td>{{$student->studname}}</td>
                  @foreach($stud_attend as $stud_attends)
                  @if($student->id == $stud_attends->studid)  
                  @if($stud_attends->attendence == 1)
                  <td><font color="green" size="3">p</font></td>
                  @elseif($stud_attends->attendence == 0)
                  <td><font color="red" size="3">a</font></td>   
                  @endif
                  @endif                 
                  @endforeach
                  <td>{{$presentsCountQuery}}</td>
                  <td>{{$absentsCountQuery}}</td>
                  </tr>          
                  @endif                 
                  @endforeach

我也在添加此代码

foreach($students as $student){
            $cunt = DB::table($wys_attendence_table)
                 ->where('studid',$student->id)
                 ->where('amonth',$amonth)
                 ->where('ayear',$ayear) 
                 ->where('attendence','=',1)
                 ->count();
            var_dump($cunt);      
        } 

进入我的控制器页面,检查礼物和缺席的总数。

我拿var_dump得到他回答int(6)int(6)int(5)

但我无法在视图页面上显示

如何在我的控制器页面中检查每个学生的当前总数(出勤率= 1)和缺席总数(出勤率= 0),以及如何显示值以查看页面?

这是我的表wys_attendence:

id   studid  adate  amonth  ayear  acls_id  attendence      
1    28      02     07      2015   10       1     
2    31      02     07      2015   10       0  
4    32      02     07      2015   10       1   
5    28      30     07      2015   10       0 
6    31      30     07      2015   10       1   
7    32      30     07      2015   10       1   
9    28      31     07      2015   10       1   
10   31      31     07      2015   10       1   
11   32      31     07      2015   10       1   
13   28      06     08      2015   10       1   
14   31      06     08      2015   10       0   
15   32      06     08      2015   10       1   
17   28      07     08      2015   10       0   
18   31      07     08      2015   10       1   
19   32      07     08      2015   10       1   
21   28      08     08      2015   10       1   
22   31      08     08      2015   10       1   
23   32      08     08      2015   10       0   
24   28      12     08      2015   10       1   
25   31      12     08      2015   10       1   
26   32      12     08      2015   10       0  

2 个答案:

答案 0 :(得分:0)

试试这个@cnbwd:

{
  "images" : [
    {
      "extent" : "full-screen",
      "idiom" : "iphone",
      "subtype" : "736h",
      "filename" : "LaunchImage-800-Portrait-736h@3x.png",
      "minimum-system-version" : "8.0",
      "orientation" : "portrait",
      "scale" : "3x"
    },
    {
      "extent" : "full-screen",
      "idiom" : "iphone",
      "subtype" : "667h",
      "filename" : "LaunchImage-800-667h@2x.png",
      "minimum-system-version" : "8.0",
      "orientation" : "portrait",
      "scale" : "2x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "iphone",
      "extent" : "full-screen",
      "minimum-system-version" : "7.0",
      "filename" : "LaunchImage-700@2x.png",
      "scale" : "2x"
    },
    {
      "extent" : "full-screen",
      "idiom" : "iphone",
      "subtype" : "retina4",
      "filename" : "LaunchImage-700-568h@2x.png",
      "minimum-system-version" : "7.0",
      "orientation" : "portrait",
      "scale" : "2x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "minimum-system-version" : "7.0",
      "filename" : "LaunchImage-700-Portrait~ipad.png",
      "scale" : "1x"
    },
    {
      "orientation" : "landscape",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "minimum-system-version" : "7.0",
      "filename" : "LaunchImage-700-Landscape~ipad.png",
      "scale" : "1x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "minimum-system-version" : "7.0",
      "filename" : "LaunchImage-700-Portrait@2x~ipad.png",
      "scale" : "2x"
    },
    {
      "orientation" : "landscape",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "minimum-system-version" : "7.0",
      "filename" : "LaunchImage-700-Landscape@2x~ipad.png",
      "scale" : "2x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "iphone",
      "extent" : "full-screen",
      "filename" : "LaunchImage-700.png",
      "scale" : "1x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "iphone",
      "extent" : "full-screen",
      "filename" : "LaunchImage-700@2x.png",
      "scale" : "2x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "iphone",
      "extent" : "full-screen",
      "filename" : "LaunchImage-700-568h@2x.png",
      "subtype" : "retina4",
      "scale" : "2x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "filename" : "LaunchImage-700-Portrait~ipad.png",
      "scale" : "1x"
    },
    {
      "orientation" : "landscape",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "filename" : "LaunchImage-700-Landscape~ipad.png",
      "scale" : "1x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "filename" : "LaunchImage-700-Portrait@2x~ipad.png",
      "scale" : "2x"
    },
    {
      "orientation" : "landscape",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "filename" : "LaunchImage-700-Landscape@2x~ipad.png",
      "scale" : "2x"
    }
  ],
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}

答案 1 :(得分:0)

试试这个

$attendance = DB::table($wys_attendence_table)
->select(DB::raw('count(attendence) as count_attendance'))
->where('amonth',$amonth)
->where('ayear',$ayear)
->groupBy('adate')
->get();

修改

您可以从两个查询中进行一次查询,并在一个foreach中使用它。 像这样:

$countQuery = DB::table($wys_attendence_table)
    ->select(DB::raw('studid, COUNT(*) AS counter'))
    ->groupBy('studid')
    ->toSql();

$stud_attend = DB::table($wys_attendence_table . ' AS table')
    ->leftJoin(DB::raw('(' . $countQuery . ') AS counter_table'), 'table.studid', '=', 'counter_table.studid')
    ->where('amonth',$amonth)
    ->where('ayear',$ayear)
    ->groupBy('adate')
    ->get();

EDIT2:

$presentsCountQuery = DB::table($wys_attendence_table)
    ->select(DB::raw('studid, COUNT(*) AS presents_counter'))
    ->whereRaw('attendence = 1')
    ->groupBy('studid')
    ->toSql();

$absentsCountQuery = DB::table($wys_attendence_table)
    ->select(DB::raw('studid, COUNT(*) AS absents_counter'))
    ->whereRaw('attendence = 0')
    ->groupBy('studid')
    ->toSql();

$stud_attend = DB::table($wys_attendence_table . ' AS table')
    ->leftJoin(DB::raw('(' . $presentsCountQuery . ') AS presents_counter_table'), 'table.studid', '=', 'presents_counter_table.studid')
    ->leftJoin(DB::raw('(' . $absentsCountQuery . ') AS absents_counter_table'), 'table.studid', '=', 'absents_counter_table.studid')
    ->where('amonth',$amonth)
    ->where('ayear',$ayear)
    ->groupBy('adate')
    ->get();

EDIT3:

完整代码:

$attendance = DB::table($wys_attendence_table)
            ->whereRaw('amonth = "'.$amonth.'"')
            ->whereRaw('ayear = "'.$ayear.'"')
            ->groupBy('adate')
            ->get();

$presentsCountQuery = DB::table($wys_attendence_table)
            ->select(DB::raw('studid, COUNT(*) AS presents_counter'))
            ->whereRaw('amonth = "'.$amonth.'"')
            ->whereRaw('ayear = "'.$ayear.'"')
            ->whereRaw('attendence = 1')
            ->groupBy('studid')
            ->toSql();

$absentsCountQuery = DB::table($wys_attendence_table)
            ->select(DB::raw('studid, COUNT(*) AS absents_counter'))
            ->whereRaw('amonth = "'.$amonth.'"')
            ->whereRaw('ayear = "'.$ayear.'"')
            ->whereRaw('attendence = 0')
            ->groupBy('studid')
            ->toSql();

$stud_attend = DB::table($wys_attendence_table . ' as table')
            ->leftJoin(DB::raw('(' . $presentsCountQuery . ') AS presents_counter_table'), 'table.studid', '=', 'presents_counter_table.studid')
            ->leftJoin(DB::raw('(' . $absentsCountQuery . ') AS absents_counter_table'), 'table.studid', '=', 'absents_counter_table.studid')
            ->where('table.amonth',$amonth)
            ->where('table.ayear',$ayear)
            ->groupBy('table.studid')
            ->get();
var_dump($stud_attend);