Tuesday, April 19, 2022

[FIXED] How to do math operations in the Controller and then display them in blade

Issue

I am at a low level of Laravel and PHP advancement. All my knowledge is based on YouTube tutorials, Laravel documentation and online forums.

To deepen my knowledge, I decided to create an application project that would calculate the price based on constant values ​​and variables (entered by the user). The constants are in one table, they are put in there by me and cannot be changed. Variable values ​​are entered by the user.

At the moment, I managed to make a connection to the database (full CRUD), however, I cannot cope with the formula for calculating many records from two tables. I don't know how to perform math calculations in Controller and then display them in Blade.

I do not expect ready code, but only advice on what method to use or whether I made a mistake while creating the tables. I am asking for help and thank you to those who made it to the end of this post.

The first table with constant values ​​looks like this: constant_costs

id cost_name amount_of_cost
1 Tax_01 1.36
2 Tax_02 0.15
3 Tax_03 0.08
4 Transport 0.37
5 Margin_01 0.26
6 Margin_02 0.10

The second table, the value of which is added by the user by the form (I entered some sample numbers below): stuff

id value_01 value_02 value_03
1 100 4 20

The formula for the price is a bit complicated: (((value_01 / 159 + Margin_01) * value_02) + Tax_01 + Tax_02 + Tax_03 + Transport + Margin_02) * value_03

When I put the accessor in the Model, I get an error: BadMethodCallException Method Illuminate \ Database \ Eloquent \ Collection :: getFormulaCalculation does not exist.

In my Model
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;

class stuff extends Model
{
    use HasFactory;

    protected $primaryKey = 'id';

    public $timestamps = FALSE;

    protected $fillable = ['value_01', 'value_02', 'value_03'];

    /**
     * Price formula
     *
     * @return \Illuminate\Database\Eloquent\Casts\Attribute
     */
    public function getFormulaCalculationAttribute(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => (($this->value_01 / 159 + 0.26) * $this->value_02) + 1.369 + 0.15261 + 0.08 + 0.37 + 0.1 * $this->value_03);
    }
}

In my Controller
use Illuminate\Http\Request;
use App\Models\stuff;

public function result()
    {
        public function result()
    {
        $stuff = stuff::all();
        $getFormulaCalculationAttribute = $stuff->getFormulaCalculation();

        return view('stuff.result')->with('getFormulaCalculationAttribute', $getFormulaCalculationAttribute);
    }
    }

Solution

I didn't get the idea from creating a table for constant values, there is a simple solution to create this calculation in your Eloquent model.

you can create an Accessor for your formula something like this

public function getFormulaCalculationAttribute()
{
    return ((($this->value_01 / 159 + Margin_01) * $this->value_02) + Tax_01 + Tax_02 + Tax_03 + Transport + Margin_02) * $this->value_03;
}

just replace the constant with your values

after that, you can call this attribute like this

echo $value->formula_calculation;

please pay attention to your laravel version starting from laravel 9 defining an accessor is different https://laravel.com/docs/8.x/eloquent-mutators#defining-an-accessor

or for laravel 9

https://laravel.com/docs/9.x/eloquent-mutators#defining-an-accessor

I hope it's helpful



Answered By - Ahmed Hassan
Answer Checked By - Robin (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.