如何在 Controller 中进行数学运算,然后在 blade 中显示它们

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

我处于Laravel和PHP进步的低水平。我所有的知识都基于 YouTube 教程、Laravel 文档和在线论坛。

为了加深我的知识,我决定创建一个应用程序项目,它会根据常数值和变量(由用户输入)来计算价格。常量在一个table里,是我放在那里的,不能改。变量值由用户输入。

目前,我设法连接到数据库(完整的 CRUD),但是,我无法处理从两个 tables 计算许多记录的公式。我不知道如何在 Controller 中执行数学计算,然后在 Blade 中显示它们。

我不期望现成的代码,而只是关于使用什么方法或者我在创建 table 时是否犯了错误的建议。我正在寻求帮助,感谢那些坚持到最后的人 post。

常量值的第一个table是这样的: 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

第二个table,其值由用户通过表单添加(我在下面输入了一些示例数字): 东西

id value_01 value_02 value_03
1 100 4 20

价格的公式有点复杂: (((value_01 / 159 + Margin_01) * value_02) + Tax_01 + Tax_02 + Tax_03 + 运输 + Margin_02 ) * value_03

当我将访问器放入模型时,出现错误: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);
    }
    }

我没有想到为常量值创建 table,有一个简单的解决方案可以在您的 Eloquent 模型中创建此计算。

您可以像这样为您的公式创建一个访问器

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;
}

只需用您的值替换常量

之后,你可以这样调用这个属性

echo $value->formula_calculation;

请注意从laravel 9开始的laravel版本定义访问器是不同的 https://laravel.com/docs/8.x/eloquent-mutators#defining-an-accessor

或 laravel 9

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

希望对您有所帮助

问题已解决。我没有将价格公式放入控制器中,而是将其放入模型中。然后我在 Laravel Blade 中提到了它。感谢您的帮助

Model
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class stuff extends Model
{
    use HasFactory;

    protected $primaryKey = 'id';

    public $timestamps = FALSE;

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

    
     //Price formula
     public function PriceFormula()
     {
        return ((($this->value_01 / 159 + 0.26) * $this->value_02) + 1.369 + 0.15261 
        + 0.08 + 0.37 + 0.1) * $this->value_03 / 100 + 1);
     }
}

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

public function result()
    {
        public function result()
        $stuff = stuff::all();
        return view('stuff.result')->with('stuff', $stuff);
    }
}

Laravel Blade
@foreach ($stuff as $stuf)
    {{ $stuf->PriceFormula() }}
@endforeach