Yes, thats way too much php in the view.
You should be doing this in the controller, or ideally in a separate class that deals with the calculations, and then just pass the display data to the view.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have the following query in controller...
$employees = Employee::with('timepunch', 'PayRate')
->whereHas('timepunch', function($q) use($start, $end){
$q->whereRaw("time_in >= '$start' AND time_in <= '$end'");
})
->whereHas('PayRate', function($q) use($start, $end){
$q->where('status', '1');
})
->where ('status', '=', '1')
->orderBy('last_name')
->get();
The Pay Rate returns a list of active pay rates for an employee.
I am trying to calculate in the view the employees straight time pay and overtime pay. I already have the hours calculating now I need to take the straight time hours and multiply them to the correct pay rate.
table layout for pay rate is
user_id , pay_type, rate, status
Straight time rate_type is = 1
Overtime rate_type is = 2.
Is there a way to do this in the view or am I way off base here....
My only other thought is to create a second query to only obtain straight pay rate then one for over time pay rate.
Here is the view
@extends('layouts.app')
@section('page-title', 'Accounting Dashborad')
@section('content')
<div class="row">
<div class="col-8">
<h1>Payroll Report</h1>
</div>
<div class="col-sm-4 ">
<table>
<thead>
<tr>
<th>Report Type</th>
<td>All Active Employee Payroll</td>
</tr>
<tr>
<th>Start Date</th>
<td></td>
</tr>
<tr>
<th>End Date</th>
<td></td>
</tr>
<tr>
<th>Report Ran By</th>
<td>Joshua Blevins</td>
</tr>
</thead>
</table>
</div>
</div>
@foreach($employees as $row)
<?php
$wk1tot = 0;
$wk2tot = 0;
$wk1st = 0;
$wk2st = 0;
$wk1ot = 0;
$wk2ot = 0;
$ppst = 0;
$ppot = 0;
?>
<div class="row">
<div class="col-12">
{{$row->first_name}} {{$row->middle_name}} {{$row->last_name}}
</div>
@if(count($row->timepunch))
<?php
$ppstart = $start;
$wk1end = strtotime($ppstart . '+ 6 days');
$ppend = $end;
$wk2start = strtotime($ppend . '- 6 days');
$ppstart = strtotime($ppstart);
$ppend = strtotime($ppend);
?>
@foreach($row->timepunch as $add)
<?php
if ($add->time_out === NULL){
}else {
$time_in = strtotime($add->time_in);
$time_out = strtotime($add->time_out);
if ($time_in >= $start && $time_in <= $wk1end) {
$shifthours = $time_out - $time_in;
$wk1tot = $wk1tot + $shifthours / (60 * 60);
if ($wk1tot > 40) {
$wk1st = 40;
$wk1ot = $wk1tot - 40;
} elseif ($wk1tot <= 0) {
$wk1ot = 0;
$wk1st = $wk1tot;
} else {
$wk1ot = 0;
$wk1st = $wk1tot;
}
} elseif ($time_in >= $wk2start && $time_in <= $ppend) {
if ($add->time_out === NULL) {
$shifthours = 0;
} else {
$shifthours = $time_out - $time_in;
$wk2tot = $wk2tot + $shifthours / (60 * 60);
if ($wk2tot > 40) {
$wk2st = 40;
$wk2ot = $wk2tot - 40;
} elseif ($wk2tot <= 0) {
$wk2ot = 0;
$wk2st = $wk1tot;
} else {
$wk2ot = 0;
$wk2st = $wk2tot;
}
}
}
}
?>
@endforeach
<?php
$ppst = $wk1st + $wk2st;
$ppot = $wk1ot + $wk2ot;
$stpay = $ppst * 16.50;
$otpay = $ppot * 24.75;
$deposit = $stpay + $otpay;
?>
@endif
<div class="col-12">
<table class="table table">
<thead>
<tr>
<th>ST</th> <th>OT</th> <th>VAMC</th> <th>PT</th> <th>FED</th> <th>SS</th> <th>MED</th> <th>STATE</th> <th>CITY</th> <th>CHILD</th> <th>DEDUCTION</th> <th>DEPOSIT</th>
</tr>
</thead>
<tbody>
<tr>
<td>${{$stpay}}</td> <td>${{$otpay}}</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>${{$deposit}}</td>
</tr>
</tbody>
</table>
</div>
</div>
@endforeach
@stop
@section('styles')
@stop
@section('scripts')
@stop
Please or to participate in this conversation.