lundi 25 décembre 2017

Laravel - WHERE Clause with Relationship to Another Table

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','>','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','>','SELECT SUM(payments_distributions.amount) FROM payments_distributions WHERE payments_distributions.shipment_id = pro_number'],
    ]

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

Thanks! Matthew



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire