mardi 30 avril 2019

Laravel get every query for a gift card in order to set the residual amount

I've a gift card Model and I want to retrieve every time it has been used to determine the residual amount. Here is my GiftCardController :

public function index()
{
    //
    $tousLesBons = BonCadeau::with(['user', 'moyendepaiement'])->orderBy('created_at', 'desc')->get();

    $changed = $tousLesBons->map(function ($value, $key) {
        $value['residualAmount'] = 0;
        return $value;
    });

    return $changed->all();
}

This works properly but instead of setting the residualAmount property to 0 which I add with mapping (not a column in my database), I want to take the gift card's base amount. So if I have a gift card of 500$, I would take those 500$ and substract every time transaction the owner made.

Therefor I need to query all database rows related to the gift card and substract them to the initial amount until it hits 0$. The model has a relation to ('App\Items') so I should be able to use it.

Thanks for reading



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2DGd5t7
via IFTTT

Aucun commentaire:

Enregistrer un commentaire