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

mprythero's avatar

Laravel - WHERE Clause with Relationship to Another Table (DB::raw??)

I've got a couple of tables in my Laravel application, one called shipments and the other called payments_distributions.

In my shipments, I have columns called pro_number and balance.

In my payments_distributions I have columns called shipment_id and amount.

Now I have a controller which has this portion of code in it:

    }elseif($_GET['paymentStatus']=="Unpaid"){
        if(empty($_GET['pro_number'])){
                $shipment = NULL;
            }else{
            $shipment = $_GET['pro_number'];
            }
            
            if($_GET['startDate']){
                $startDate = $_GET['startDate'];
            }
            if($_GET['endDate']){
                $endDate = $_GET['endDate'];
            }
                    $start = $_GET['startDate'];
                    $end = $_GET['endDate'];
                    $status = $_GET['paymentStatus'];
        $date = \Carbon\Carbon::today()->subDays(0);
    
        $shipments = Shipment::sortable()
        ->where([
            ['due_date','<=',date($date)],
            ['pro_number', 'LIKE', '%' . $shipment . '%'],
            ['balance','>',DB::raw('SELECT SUM(payments_distributions.amount) FROM payments_distributions WHERE payments_distributions.shipment_id = pro_number')],
        ])
        ->whereBetween('date', [$startDate, $endDate])
        ->whereNotIn('shipment_billing_status', [2,3])
        ->paginate(25);
        return view('shipments.accounts', compact('shipments','start','end','status'));
        
    }

Now my issue arises in that the following code works if I put it into MYSQL:

    SELECT SUM(payments_distributions.amount) FROM payments_distributions WHERE payments_distributions.shipment_id = 214050

Will return SUM(payments_distributions.amount): 300.00

So, if you go to the line in my code, this is the only thing that doesn't work, so if you could, does this line of code look right to everyone? All I'm trying to say that the balance field from a shipment row must be larger than the sum of amount fields in the payment_distributions table where the shipment_id field in the payment_distributions table equals the pro_number field in the shipments table.

     ['balance','>',DB::raw('SELECT SUM(payments_distributions.amount) FROM payments_distributions WHERE payments_distributions.shipment_id = pro_number')],
        ]

My full error in fact is:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT SUM(p.amount) FROM payments_distributions p WHERE p.shipment_id = shipmen' at line 1 (SQL: select count(*) as aggregate from `shipments` where (`due_date` <= 2017-12-26 00:00:00 and `pro_number` LIKE %% and `balance` > SELECT SUM(p.amount) FROM payments_distributions p WHERE p.shipment_id = shipments.pro_number) and `date` between 1997-12-01 and 2017-12-25 and `shipment_billing_status` not in (2, 3))

And I will point this out, everything else works, it's just this balance issue that is causing the problems.

Thank you so much for all of the help in advance, I greatly appreciate it!

Thanks! Matthew

----UPDATE (THE ENTIRE FUNCTION) ------

public function accountsQuery(){
        

if(($_GET['paymentStatus'] == "No Settle") or ($_GET['paymentStatus'] == "No Charge")){
            if($_GET['paymentStatus'] == "No Settle"){
                $status = 3;
            }elseif($_GET['paymentStatus'] == "No Charge"){
                $status = 2;
            }
                if(empty($_GET['pro_number'])){
                $shipment = NULL;
                }else{
                $shipment = $_GET['pro_number'];
                }

                if($_GET['startDate']){
                    $startDate = $_GET['startDate'];
                }
                if($_GET['endDate']){
                    $endDate = $_GET['endDate'];
                }
                        $start = $_GET['startDate'];
                        $end = $_GET['endDate'];
                        $status = $_GET['paymentStatus'];
                        $shipments = \App\Shipment::sortable()->where([ 
                            ['pro_number', 'LIKE', '%' . $shipment . '%'],
                            ['shipment_billing_status', 'LIKE', '%' . $status . '%'],
                        ])
                            ->whereBetween('date', [$startDate, $endDate])
                            ->paginate(25);
                return view('shipments.accounts', compact('shipments','start','end','status'));
    
}elseif($_GET['paymentStatus']=="Billed"){
    if(empty($_GET['pro_number'])){
            $shipment = NULL;
        }else{
        $shipment = $_GET['pro_number'];
        }
        
        if($_GET['startDate']){
            $startDate = $_GET['startDate'];
        }
        if($_GET['endDate']){
            $endDate = $_GET['endDate'];
        }
                $start = $_GET['startDate'];
                $end = $_GET['endDate'];
                $status = $_GET['paymentStatus'];
    $date = \Carbon\Carbon::today()->subDays(30);
    //AND TO MENTION WHERE SUM OF PAYMENTS TO THIS SHIPMENT < BALANCE DUE
    $shipments = Shipment::sortable()
    ->where([
        ['date','>=', date($date)],
        ['pro_number', 'LIKE', '%' . $shipment . '%'],
    ])
    ->whereBetween('date', [$startDate, $endDate])
    ->paginate(25);
    return view('shipments.accounts', compact('shipments','start','end','status'));
    
}elseif($_GET['paymentStatus']=="Unpaid"){
    if(empty($_GET['pro_number'])){
            $shipment = NULL;
        }else{
        $shipment = $_GET['pro_number'];
        }
        
        if($_GET['startDate']){
            $startDate = $_GET['startDate'];
        }
        if($_GET['endDate']){
            $endDate = $_GET['endDate'];
        }
                $start = $_GET['startDate'];
                $end = $_GET['endDate'];
                $status = $_GET['paymentStatus'];
    $date = \Carbon\Carbon::today()->subDays(0);

    $shipments = Shipment::sortable()
    ->where([
        ['due_date','<=',date($date)],
        ['pro_number', 'LIKE', '%' . $shipment . '%'],
        ['balance','>',DB::raw('SELECT SUM(payments_distributions.amount) FROM payments_distributions WHERE payments_distributions.shipment_id = pro_number')],
    ])
    ->whereBetween('date', [$startDate, $endDate])
    ->whereNotIn('shipment_billing_status', [2,3])
    ->paginate(25);
    return view('shipments.accounts', compact('shipments','start','end','status'));
    
}  
}
0 likes
0 replies

Please or to participate in this conversation.