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

joshblevins's avatar

Getting a value from query on blade via further query

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



0 likes
5 replies
Snapey's avatar

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.

joshblevins's avatar

Yup most probably in over my head..... I’ll have try o do some research on your recommendation.

As for getting the pay rates will that have to be two separate queries or can it be together like it is

Snapey's avatar

You should also extract any 'magic numbers' out to a config file or into a database table so that you don't need to release new code if a rate changes.

By 'magic numbers' I mean things like;

$stpay = $ppst * 16.50;

or for instance, if the working week changes from 40 hours to 37.5

joshblevins's avatar

The pay rates are actually in the dB table however are joined to the employee table because each employee has multiple rates. And I wanted to be able to track pay rate changes.

So I did hard code a rate in testing the code. I ran into a spot getting each employees pay rate from the query for example.

Employee A worked 40 straight hours with 10 overtime hours.

Now from the pay rates table... how could I get the straight pay which to multiply by 40 and overtime to multiply by 10?

joshblevins's avatar

Pondering I could do a loop on pay rates and an if statement then multiply each category as needed...

Please or to participate in this conversation.