Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdulBazith's avatar

Display attendance report in laravel blade file

Guys i have an employee module which makes attendance for them.

the emp_attendance table has columns

att_date
emp_id
att_count
reason

//like below

att_date        emp_id      att-count       reason
01/07           1           1               
01/07           2           0           Leave fever 
02/07           1           0.5         halfday fever
02/07           2           1

so my query is

  $empatts = EmpAttendance::

    where(function ($query) use ($request) {

        if (!empty($request->from_date))
         {
         $query->whereBetween('att_date', [$request->from_date, $request->to_date]);
        }    

       })->orderBy('created_at','asc')->get();

and when i give dd($empatts );

Collection {#1797 ▼
  #items: array:189 [▼
    0 => EmpAttendance {#1607 ▼
      #fillable: array:6 [ …6]
      #connection: "mysql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:10 [ …10]
      #original: array:10 [ …10]
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #events: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [ …1]
    }
    1 => EmpAttendance {#1608 ▼
      #fillable: array:6 [ …6]
      #connection: "mysql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:10 [ …10]
      #original: array:10 [ …10]
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #events: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [ …1]
    }
    2 => EmpAttendance {#1609 ▶}
    3 => EmpAttendance {#1610 ▶}
    4 => EmpAttendance {#1611 ▶}
    5 => EmpAttendance {#1612 ▶}
    6 => EmpAttendance {#1613 ▶}
    7 => EmpAttendance {#1614 ▶}
    8 => EmpAttendance {#1615 ▶}
    9 => EmpAttendance {#1616 ▶}
    10 => EmpAttendance {#1617 ▶}
    11 => EmpAttendance {#1618 ▶}
    12 => EmpAttendance {#1619 ▶}
    13 => EmpAttendance {#1620 ▶}
    14 => EmpAttendance {#1621 ▶}
    15 => EmpAttendance {#1622 ▶}
    16 => EmpAttendance {#1623 ▶}
    17 => EmpAttendance {#1624 ▶}
    18 => EmpAttendance {#1625 ▶}
    19 => EmpAttendance {#1626 ▶}
    20 => EmpAttendance {#1627 ▶}
    21 => EmpAttendance {#1628 ▶}
    22 => EmpAttendance {#1629 ▶}
    23 => EmpAttendance {#1630 ▶}
    24 => EmpAttendance {#1631 ▶}
    25 => EmpAttendance {#1632 ▼
      #fillable: array:6 [ …6]
      #connection: "mysql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:10 [ …10]
      #original: array:10 [ …10]
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #events: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [ …1]
    }
    26 => EmpAttendance {#1633 ▶}
    27 => EmpAttendance {#1634 ▶}
    28 => EmpAttendance {#1635 ▶}
    29 => EmpAttendance {#1636 ▶}
    30 => EmpAttendance {#1637 ▶}
    31 => EmpAttendance {#1638 ▶}
    32 => EmpAttendance {#1639 ▶}
    33 => EmpAttendance {#1640 ▶}
    34 => EmpAttendance {#1641 ▶}
    35 => EmpAttendance {#1642 ▶}
    36 => EmpAttendance {#1643 ▶}
    37 => EmpAttendance {#1644 ▶}
    38 => EmpAttendance {#1645 ▶}
    39 => EmpAttendance {#1646 ▶}
    40 => EmpAttendance {#1647 ▶}
    41 => EmpAttendance {#1648 ▶}
    42 => EmpAttendance {#1649 ▶}
    43 => EmpAttendance {#1650 ▶}
    44 => EmpAttendance {#1651 ▶}
    45 => EmpAttendance {#1652 ▶}
    46 => EmpAttendance {#1653 ▶}
    47 => EmpAttendance {#1654 ▶}
    48 => EmpAttendance {#1655 ▶}
    49 => EmpAttendance {#1656 ▶}
    50 => EmpAttendance {#1657 ▶}
    51 => EmpAttendance {#1658 ▶}
    52 => EmpAttendance {#1659 ▶}
    53 => EmpAttendance {#1660 ▶}
    54 => EmpAttendance {#1661 ▶}
    55 => EmpAttendance {#1662 ▶}
    56 => EmpAttendance {#1663 ▶}
    57 => EmpAttendance {#1664 ▶}
    58 => EmpAttendance {#1665 ▶}
    59 => EmpAttendance {#1666 ▶}
    60 => EmpAttendance {#1667 ▶}
    61 => EmpAttendance {#1668 ▶}
    62 => EmpAttendance {#1669 ▶}
    63 => EmpAttendance {#1670 ▶}
    64 => EmpAttendance {#1671 ▶}
    65 => EmpAttendance {#1672 ▶}
    66 => EmpAttendance {#1673 ▶}
    67 => EmpAttendance {#1674 ▶}
    68 => EmpAttendance {#1675 ▶}
    69 => EmpAttendance {#1676 ▶}
    70 => EmpAttendance {#1677 ▶}
    71 => EmpAttendance {#1678 ▶}
    72 => EmpAttendance {#1679 ▶}
    73 => EmpAttendance {#1680 ▶}
    74 => EmpAttendance {#1681 ▶}
    75 => EmpAttendance {#1682 ▶}
    76 => EmpAttendance {#1683 ▶}
    77 => EmpAttendance {#1684 ▶}
    78 => EmpAttendance {#1685 ▶}
    79 => EmpAttendance {#1686 ▶}
    80 => EmpAttendance {#1687 ▶}
    81 => EmpAttendance {#1688 ▶}
    82 => EmpAttendance {#1689 ▶}
    83 => EmpAttendance {#1690 ▶}
    84 => EmpAttendance {#1691 ▶}
    85 => EmpAttendance {#1692 ▶}
    86 => EmpAttendance {#1693 ▶}
    87 => EmpAttendance {#1694 ▶}
    88 => EmpAttendance {#1695 ▶}
    89 => EmpAttendance {#1696 ▶}
    90 => EmpAttendance {#1697 ▶}
    91 => EmpAttendance {#1698 ▶}
    92 => EmpAttendance {#1699 ▶}
    93 => EmpAttendance {#1700 …25}
    94 => EmpAttendance {#1701 …25}
    95 => EmpAttendance {#1702 …25}
    96 => EmpAttendance {#1703 …25}
    97 => EmpAttendance {#1704 …25}
    98 => EmpAttendance {#1705 …25}
    99 => EmpAttendance {#1706 …25}
    100 => EmpAttendance {#1707 …25}
    101 => EmpAttendance {#1708 …25}
    102 => EmpAttendance {#1709 …25}
    103 => EmpAttendance {#1710 …25}
    104 => EmpAttendance {#1711 …25}
    105 => EmpAttendance {#1712 …25}
    106 => EmpAttendance {#1713 …25}
    107 => EmpAttendance {#1714 …25}
    108 => EmpAttendance {#1715 …25}
    109 => EmpAttendance {#1716 …25}
    110 => EmpAttendance {#1717 …25}
    111 => EmpAttendance {#1718 …25}
    112 => EmpAttendance {#1719 …25}
    113 => EmpAttendance {#1720 …25}
    114 => EmpAttendance {#1721 …25}
    115 => EmpAttendance {#1722 …25}
    116 => EmpAttendance {#1723 …25}
    117 => EmpAttendance {#1724 …25}
    118 => EmpAttendance {#1725 …25}
    119 => EmpAttendance {#1726 …25}
    120 => EmpAttendance {#1727 …25}
    121 => EmpAttendance {#1728 …25}
    122 => EmpAttendance {#1729 …25}
    123 => EmpAttendance {#1730 …25}
    124 => EmpAttendance {#1731 …25}
    125 => EmpAttendance {#1732 …25}
    126 => EmpAttendance {#1733 …25}
    127 => EmpAttendance {#1734 …25}
    128 => EmpAttendance {#1735 …25}
    129 => EmpAttendance {#1736 …25}
    130 => EmpAttendance {#1737 …25}
    131 => EmpAttendance {#1738 …25}
    132 => EmpAttendance {#1739 …25}
    133 => EmpAttendance {#1740 …25}
    134 => EmpAttendance {#1741 …25}
    135 => EmpAttendance {#1742 …25}
    136 => EmpAttendance {#1743 …25}
    137 => EmpAttendance {#1744 …25}
    138 => EmpAttendance {#1745 …25}
    139 => EmpAttendance {#1746 …25}
    140 => EmpAttendance {#1747 …25}
    141 => EmpAttendance {#1748 …25}
    142 => EmpAttendance {#1749 …25}
    143 => EmpAttendance {#1750 …25}
    144 => EmpAttendance {#1751 …25}
    145 => EmpAttendance {#1752 …25}
    146 => EmpAttendance {#1753 …25}
    147 => EmpAttendance {#1754 …25}
    148 => EmpAttendance {#1755 …25}
    149 => EmpAttendance {#1756 …25}
    150 => EmpAttendance {#1757 …25}
    151 => EmpAttendance {#1758 …25}
    152 => EmpAttendance {#1759 …25}
    153 => EmpAttendance {#1760 …25}
    154 => EmpAttendance {#1761 …25}
    155 => EmpAttendance {#1762 …25}
    156 => EmpAttendance {#1763 …25}
    157 => EmpAttendance {#1764 …25}
    158 => EmpAttendance {#1765 …25}
    159 => EmpAttendance {#1766 …25}
    160 => EmpAttendance {#1767 …25}
    161 => EmpAttendance {#1768 …25}
    162 => EmpAttendance {#1769 …25}
    163 => EmpAttendance {#1770 …25}
    164 => EmpAttendance {#1771 …25}
    165 => EmpAttendance {#1772 …25}
    166 => EmpAttendance {#1773 …25}
    167 => EmpAttendance {#1774 …25}
    168 => EmpAttendance {#1775 …25}
    169 => EmpAttendance {#1776 …25}
    170 => EmpAttendance {#1777 …25}
    171 => EmpAttendance {#1778 …25}
    172 => EmpAttendance {#1779 …25}
    173 => EmpAttendance {#1780 …25}
    174 => EmpAttendance {#1781 …25}
    175 => EmpAttendance {#1782 …25}
    176 => EmpAttendance {#1783 …25}
    177 => EmpAttendance {#1784 …25}
    178 => EmpAttendance {#1785 …25}
    179 => EmpAttendance {#1786 …25}
    180 => EmpAttendance {#1787 …25}
    181 => EmpAttendance {#1788 …25}
    182 => EmpAttendance {#1789 …25}
    183 => EmpAttendance {#1790 …25}
    184 => EmpAttendance {#1791 …25}
    185 => EmpAttendance {#1792 …25}
    186 => EmpAttendance {#1793 …25}
    187 => EmpAttendance {#1794 …25}
    188 => EmpAttendance {#1795 …25}


i need a out put like below

empName 1/7     2/7     3/7     4/7     5/7     total
A           1       0       0.5     1       1       3.5
B           1       0       0       0       1       2

//Instead of numbers i can make it as tick or cross symbol

but i cant get the out put plz kindly some one help

0 likes
18 replies
mstrauss's avatar

Try something like:

       $empatts = EmpAttendance::where(function ($query) use ($request) {

            if (! empty($request->from_date))
            {
                $query->whereBetween('att_date', [$request->from_date, $request->to_date]);
            }

        })->get()->groupBy('att_date');

Then you'll have a collection grouped by the att_date. And you can do a @foreach in the blade/view to display the results as you wish.

AbdulBazith's avatar

@mstrauss thank you for your response.

i used as per your code. now when dd($empatts) gives

Collection {#1758 ▼
  #items: array:3 [▼
    "2019-07-22" => Collection {#1755 ▼
      #items: array:63 [▼
        0 => EmpAttendance {#1563 ▼
          #fillable: array:6 [ …6]
          #connection: "mysql"
          #table: null
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:10 [ …10]
          #original: array:10 [ …10]
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #events: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #guarded: array:1 [ …1]
        }
        1 => EmpAttendance {#1564 ▶}
        2 => EmpAttendance {#1565 ▶}
        3 => EmpAttendance {#1566 ▶}
        4 => EmpAttendance {#1567 ▶}
        5 => EmpAttendance {#1568 ▶}
        6 => EmpAttendance {#1569 ▶}
        7 => EmpAttendance {#1570 ▶}
        8 => EmpAttendance {#1571 ▶}
        9 => EmpAttendance {#1572 ▶}
        10 => EmpAttendance {#1573 ▶}
        11 => EmpAttendance {#1574 ▶}
        12 => EmpAttendance {#1575 ▶}
        13 => EmpAttendance {#1576 ▶}
        14 => EmpAttendance {#1577 ▶}
        15 => EmpAttendance {#1578 ▶}
        16 => EmpAttendance {#1579 ▶}
        17 => EmpAttendance {#1580 ▶}
        18 => EmpAttendance {#1581 ▶}
        19 => EmpAttendance {#1582 ▶}
        20 => EmpAttendance {#1583 ▶}
        21 => EmpAttendance {#1584 ▶}
        22 => EmpAttendance {#1585 ▶}
        23 => EmpAttendance {#1586 ▶}
        24 => EmpAttendance {#1587 ▶}
        25 => EmpAttendance {#1588 ▶}
        26 => EmpAttendance {#1589 ▶}
        27 => EmpAttendance {#1590 ▶}
        28 => EmpAttendance {#1591 ▶}
        29 => EmpAttendance {#1592 ▶}
        30 => EmpAttendance {#1593 ▶}
        31 => EmpAttendance {#1594 ▶}
        32 => EmpAttendance {#1595 ▶}
        33 => EmpAttendance {#1596 ▶}
        34 => EmpAttendance {#1597 ▶}
        35 => EmpAttendance {#1598 ▶}
        36 => EmpAttendance {#1599 ▶}
        37 => EmpAttendance {#1600 ▶}
        38 => EmpAttendance {#1601 ▶}
        39 => EmpAttendance {#1602 ▶}
        40 => EmpAttendance {#1603 ▶}
        41 => EmpAttendance {#1604 ▶}
        42 => EmpAttendance {#1605 ▶}
        43 => EmpAttendance {#1606 ▶}
        44 => EmpAttendance {#1607 ▶}
        45 => EmpAttendance {#1608 ▶}
        46 => EmpAttendance {#1609 ▶}
        47 => EmpAttendance {#1610 ▶}
        48 => EmpAttendance {#1611 ▶}
        49 => EmpAttendance {#1612 ▶}
        50 => EmpAttendance {#1613 ▶}
        51 => EmpAttendance {#1614 ▶}
        52 => EmpAttendance {#1615 ▶}
        53 => EmpAttendance {#1616 ▶}
        54 => EmpAttendance {#1617 ▶}
        55 => EmpAttendance {#1618 ▶}
        56 => EmpAttendance {#1619 ▶}
        57 => EmpAttendance {#1620 ▶}
        58 => EmpAttendance {#1621 ▶}
        59 => EmpAttendance {#1622 ▶}
        60 => EmpAttendance {#1623 ▶}
        61 => EmpAttendance {#1624 ▶}
        62 => EmpAttendance {#1625 ▶}
      ]
    }
    "2019-07-30" => Collection {#1756 ▼
      #items: array:63 [▶]
    }
    "2019-07-28" => Collection {#1757 ▼
      #items: array:63 [▶]
    }
  ]
}

the records are grouped by date wise. i have only 3 date records. it is correct. how i can use it in my blade file plz suggest me that also

mstrauss's avatar

How about using the @foreach directive with the $key/$value like:

@foreach ($empatts as $key => $empat)

    // contains the date
    $key; 

    // contains the other pertinent data, like emp_id, att_count, etc. 
    $empat

    // you may want to loop through the $empat to display its data. 
    @foreach ($empat as $value)
        
        // employee id
        $value['emp_id`]; 
    
    @endforeach 

@endforeach

if you feel that the inner foreach is too messy for a blade file, which I kind of do, you can handle the logic in your controller using the map method on the collection instance.

AbdulBazith's avatar

@mstrauss thank you for your response. ya i treid with your code. somewhat messy only. but i made a view llike below

 <tbody>

     @php ($s_no = 1)
     <tr>
         <th width=5%> S. No</th>
         <th width=25%> Name</th>
         @foreach ($empatts as $key => $empat)
         <th width=10%>{{ $key}} </th>
         @endforeach
     </tr>

     @foreach ($empat as $value)
     <tr>
         <td> {{ $s_no  }}</td>
         <td> {{ $value->employee->emp_name}} </td>

         @foreach ($empatts as $key => $empat)
         <td>
             {{$value->att_count }}

         </td>

         @endforeach
     </tr>
     @php ($s_no++)
     @endforeach
 </tbody>


everything is fine. the view. but mistakes are there. whats the problem can u guess

refer this: https://imgur.com/E3mCMPH

Here for the selected person "MANI MANTHIRAM" i have put half day on 2019-07-01. so it must display 0.5 but it displaying 1 and for ANAND it must be 0 on that date. but 1 is there. whats the problem.

for all 1 only displaying...

i cant guess whats the problem.

Kindly suggest please...

mstrauss's avatar

@abdulbazith

I think your @foreach structure may be the issue, try this:

<tbody>

     @php ($s_no = 1)
     <tr>
         <th width=5%> S. No</th>
         <th width=25%> Name</th>
         @foreach ($empatts as $key => $empat)
         <th width=10%>{{ $key}} </th>
         @endforeach
     </tr>

     @foreach ($empat as $value)
     <tr>
         <td> {{ $s_no  }}</td>
         <td> {{ $value->employee->emp_name}} </td>
    <td>  {{$value->att_count }} </td>
     </tr>
     @php ($s_no++)
     @endforeach
 </tbody>
mstrauss's avatar

@abdulbazith

I think I see the issue, try this:

 <tbody>

     @php ($s_no = 1)
     <tr>
         <th width=5%> S. No</th>
         <th width=25%> Name</th>
         @foreach ($empatts as $key => $empat)
         <th width=10%>{{ $key}} </th>
       
     </tr>

     @foreach ($empat as $value)
     <tr>
         <td> {{ $s_no  }}</td>
         <td> {{ $value->employee->emp_name}} </td>

       
         <td>
             {{$value->att_count }}

         </td>
   @php ($s_no++)
         @endforeach
     </tr>
 
     @endforeach
 </tbody>
AbdulBazith's avatar

@mstrauss same problem.

i think we are missing with a loop some where

ok.

can i have a output like this format

Sno     Name        TotalDays       Present         Absent

1       MANI        31              25          6
2       ABDUL       31              23          8


can i get a output like this `` i my db how i stored the attendance record as, present means 1 absent means 0 halfday means 0.5

like below

att_date
emp_id
att_count
reason

//like below

att_date        emp_id      att-count       reason
01/07           1           1               
01/07           2           0           Leave fever 
02/07           1           0.5         halfday fever
02/07           2           1


mstrauss's avatar

Hi @abdulbazith

For the new format you mentioned, the below should work:

// controller

 $employees = App\Employee::withCount([
            'emp_attendance',
            'emp_attendance as present' => function (Builder $query) {
                $query->where('att-count', 1);
            }
        ])->get();

// view

                    @foreach($employees as $employee)

                        <tr>
                            <td> {{ $s_no  }}</td>
                            <td> {{ $employee->name }} </td>
                            <td> {{ $employee->emp_attendance_count }} </td>
                            <td> {{ $employee->present_count }} </td>
                            <td> {{ $employee->emp_attendance_count - $employee->present_count }} </td>
                        </tr>

                        @php ($s_no++)

                    @endforeach

There is an assumption that you have created a relationship on the Employee and the EmployeeAttendance models like below:

// Employee model

    public function employeeAttendaces()
    {
        return $this->hasMany('App\EmployeeAttendance ');
    }

// EmployeeAttendance model

    public function employee()
    {
        return $this->belongsTo('App\Employee');
    }

Please also note this will count all employee attendance records for each given employee. If you want to filter down by a given period, you would have to update the query a bit.

AbdulBazith's avatar

@mstrauss

still getting error

this is my EmpAttendance model

public function employee()
    {
        return $this->belongsTo('App\Employee', 'emp_id');
    }

this is my Employee model

public function empattendance()
    {
        return $this->hasMany('App\EmpAttendance', 'emp_id');
    }

and this is my code

$employees = Employee::withCount([
    'empattendance',
    'empattendance as present' => function (Builder $query) {
        $query->where('att_count', 1);
    }
])->get();

dd($employees);

i got this error

Type error: Argument 1 passed to App\Http\Controllers\EmpAttendanceController::App\Http\Controllers\{closure}() must be an instance of App\Http\Controllers\Builder, instance of Illuminate\Database\Eloquent\Builder given, called in C:\xampp\htdocs\Laravel\hotel_sri_ram_prasad\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php on line 934


and one important thing i dont have multiple columns for attendance count

att_date        emp_id       att-count       reason
01/07           1            1               
01/07           2               0            Leave fever 
02/07           1               0.5             halfday fever
02/07           2               1
03/07        1          0           absent  

so in the above for employee 1

the present days is 1.5 (1+0.5)

the absentdays is 1.5 (1+0.5) (here the problem is the 0 are absent so that must be count and 0.5 is half day that must be sum and then both must be added ). so the count of 0 is 1 and the sum of 0.5 is 0.5 so adding both 1.5 is the absent days

total days are 3 (1 + 1 + 1)

mstrauss's avatar

@abdulbazith

Hmm.. it looks like you are using an _ instead of a - in the below line:

$query->where('att_count', 1);

Per your model properties, I think it should be:

$query->where('att-count', 1);

Can you try that just to see what the result is?

mstrauss's avatar

If you comment out the new code, does the error go away? I can't see how this query would cause that error.

AbdulBazith's avatar

@mstrauss

exactly i gave like this

   $employees = Employee::withCount([
    'empattendance',
    'empattendance as present' => function (Builder $query) {
        $query->where('att-count', 1);
    }
])->get();

dd($employees);

but i dont know whats the problem.

now what should i doo

1 like
mstrauss's avatar

Did you include the use App\Employee; import statement at the top of the EmpAttendanceController?

AbdulBazith's avatar

@mstrauss

yes how do i will miss that.

<?php

namespace App\Http\Controllers;

use Alert;
use App\EmpAttendance;
use App\Employee;
use Auth;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Input;
use Illuminate\Support\Facades\DB;


Please or to participate in this conversation.