Alireza Amrollahi
Alireza Amrollahi

Reputation: 915

What is the Best way to show different kind of price to one user in Laravel?

So i have this product with different kind of prices ( based on what user group you are ) ,there are so many options to show and process the prices in the client side but i can't tell which one is better . here are my thought ( any other suggestion would be a grace ) : 1- Save every price for each user group in DB and blah blah blah . 2- Have the formula and calculate in the client side ( filtering would be hard then ) 3- Save one price in DB and make other out of that .

Upvotes: 1

Views: 286

Answers (1)

user320487
user320487

Reputation:

This would probably be best handled by an accessor method defined on your model. Without knowing the specifics of how you are calculating the price, as an example, you can do something to the effect of:

// Product.php
public function getPriceAttribute($value)
{
    // calculate the price based on the user's group
    switch(auth()->user()->group) {
        case 'group1':
            return $value * .90; // 10% off
            break;
        case 'group2':
            return $value * .80; // 20% off
            break;
        case 'group3':
            return $value * .70; // 30% off
            break;
        default:
            return $value; // normal price
    }
}

The price would then be accessible like:

$product->price

Upvotes: 1

Related Questions