How to recover values in a hasMany relationship in Latin

Asked

Viewed 148 times

-1

Personal I have the following function within a Model Campaign.

  public function success_payments(){

        return $this->hasMany(Payment::class)->whereStatus('3');
    }

This function returns the Payment table values that have status = 3.

Then I use a variable to return the values of this table by summing the values of the amount fields.

$raised = $this->success_payments()->sum('amount');

Until then everything was fine, I was meeting my needs, but now there was the need to recover another field of the Payment table called updated_at that a field of type date, and with this field do a logic where I can add to this field 14 days more, and return in the $Raised variable or create another variable, only the values where the value of the updated_at field summed 14 days is greater or equal to the current date.

I don’t know if my doubt is clear.

It is that previously I used a payment gatway, and now I am using a new payment gatway where the money is released only 14 days after payment confirmation, so I need to show the User only the amounts that have the status equal to 3, that is paid, and has the status change date summing 14 days.

Thanks for your help.

1 answer

0

All relationships recover as follows:

Model::with('relacionamento')->sum('column'); //Dentro do with é o nome da função que criou no controller por exemplo

public function user() {
   return $this->hasMany(\App\User::class);
}

Model::with('user')->sum('column');

no matter which relationship did only use the with

Browser other questions tagged

You are not signed in. Login or sign up in order to post.