mardi 31 juillet 2018

Comparing Attribute in Laravel Eloquent Request

I have a controller function where I would like to compare a field in a model to an attribute of that model and use it in a where like so below, is it possible?

$ticketsOpen = Ticket::where([
                 ['balance','>','paidBalance'],
                 ['bill_to', $customer->id]
                 ])
                  ->whereNotIn('shipment_billing_status', [2,3])
                  ->orderBy('created_at','desc')->get();

In this case, the 'paidBalance' up above refers to this in the model:

public function getPaidBalanceAttribute(){
    $paid = $this->paymentDistributions->pluck('amount');
    $paidBalance = $paid->sum();
    return $paidBalance;
}

And the paymentDistributions refers above that to this:

 public function paymentDistributions(){
        return $this->hasMany('App\Payments_Distribution', 'ticket_id','id');
    }

Or is there a better way of going around this? I'm just trying to filter down the tickets that have a balance greater than what's been paid or have no payments toward the ticket at all.

Thanks!



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2M7hp6X
via IFTTT

Aucun commentaire:

Enregistrer un commentaire