mikefolu
mikefolu

Reputation: 1333

Laravel - How to get available_leave_days and include it in the current leave table in view blade

I have this code in Laravel-5.8 that displays employee leaves on table in view blade:

Controller:

public function index()
{
    $userCompany    = Auth::user()->company_id;
    $userID         = Auth::user()->id;
    $userEmployee = Auth::user()->employee_id;
    $leaverequests = HrLeaveRequest::where('employee_id', $userEmployee)->where('company_id', $userCompany)->orderBy('created_at', 'desc')->get();
      return view('leaves.index')->with('leaverequests', $leaverequests)           
}

This is rendered on the view blade

view:

        <table class=" table table-bordered table-striped table-hover datatable">
            <thead>
                <tr>

                    <th width="10">
                        #
                    </th>
                    <th>
                        Leave Type
                    </th>
                    <th>
                        Start Date
                    </th>
                    <th>
                        End Date
                    </th>  
                    <th width="18%">
                        &nbsp;
                    </th>
                </tr>
            </thead>
            <tbody>
                @foreach($leaverequests as $key => $leaverequest)
                        <td>
                            {{$key+1}}
                        </td>
                        <td>
                            {{isset($leaverequest->leavetype) ? $leaverequest->leavetype->leave_type_name : 'N/A'}}
                        </td>
                        <td>
                            {{Carbon\Carbon::parse($leaverequest->commencement_date)->format('d-m-Y') ?? '' }}                             
                        </td>
                        <td>
                            {{Carbon\Carbon::parse($leaverequest->resumption_date)->format('d-m-Y') ?? '' }} 
                        </td>  

                @endforeach 
            </tbody>
        </table>

This is working perfectly. But I want to insert on the same view blade that displays the available leave days for the employee.

Already I have the query:

$availableleavedays              = DB::table('hr_leave_requests')->select(DB::raw("IFNULL(SUM(no_of_days),0) as no_of_days"))->where('employee_id', $userEmployee)->where('leave_status', 4)->whereYear('created_at', '=', date('Y'))->first();

Since its the same table and Model Class, how do I merge;

$availableleavedays              = DB::table('hr_leave_requests')->select(DB::raw("IFNULL(SUM(no_of_days),0) as no_of_days"))->where('employee_id', $userEmployee)->where('leave_type_id', $request->id)->where('leave_status', 4)->whereYear('created_at', '=', date('Y'))->first();

to the current Query in the Controller:

 $leaverequests = HrLeaveRequest::where('employee_id', $userEmployee)->where('company_id', $userCompany)->orderBy('created_at', 'desc')->get();

So that my view blade will change to:

        <table class=" table table-bordered table-striped table-hover datatable">
            <thead>
                <tr>

                    <th width="10">
                        #
                    </th>
                    <th>
                        Leave Type
                    </th>
                    <th>
                        Start Date
                    </th>
                    <th>
                        End Date
                    </th>
                    <th>
                        Available Day
                    </th> 
                    <th width="18%">
                        &nbsp;
                    </th>
                </tr>
            </thead>
            <tbody>
                @foreach($leaverequests as $key => $leaverequest)
                        <td>
                            {{$key+1}}
                        </td>
                        <td>
                            {{isset($leaverequest->leavetype) ? $leaverequest->leavetype->leave_type_name : 'N/A'}}
                        </td>
                        <td>
                            {{Carbon\Carbon::parse($leaverequest->commencement_date)->format('d-m-Y') ?? '' }}                             
                        </td>
                        <td>
                            {{Carbon\Carbon::parse($leaverequest->resumption_date)->format('d-m-Y') ?? '' }} 
                        </td>  
                        <td>
                            {{isset($leaverequest->available_days) ? $leaverequest->available_days : 'N/A'}}
                        </td>

                @endforeach 
            </tbody>
        </table>

hr_leave_requests is the table and the model class is HrLeaveRequest

Thank you

Upvotes: 0

Views: 248

Answers (1)

nextzakir
nextzakir

Reputation: 23

Inside the last tag where you want to show the remaining leave days, you could simply do...

{{ $leaverequest->resumption_date->diffInDays(\Carbon\Carbon::now()) }}

No need to run additional queries.

Note: If you don't have Carbon, don't forget to require it first.

Upvotes: 0

Related Questions