如何在Laravel中划分两列?

时间:2018-11-02 07:36:11

标签: php laravel laravel-5

我正在毫无问题地检索每页每个ID的行。我在数据库表中有两列是扩展和价格。我想对这些列值使用除法数学。我的检索代码在下面。我也想将此代码添加除法运算! extend / price

public function show($id)
{
    $estates = allestates::where('id', $id)->first();

    return view('pages.show', ['estates' => $estates]);
}

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您应该尝试以下操作:

public function show($id)
{
    $estates = allestates::where('id', $id)->first();

    $rsltActualPrice = $estates->extend / $estates->price;

    return view('pages.show', compact('estates','rsltActualPrice'));
}

将此变量$rsltActualPrice放入视图文件中,例如:

{{$rsltActualPrice}}

OR

更改您的视图文件代码,例如:

{{$estates->extend / $estates->price}}

更新后的答案

public function show($id)
{
    $estates = allestates::where('id', $id)->first();

print('<pre style="color:red;">');
print_r($estates);
print('</pre>');
exit;

$rsltActualPrice = $estates->extend / $estates->price;

return view('pages.show', compact('estates','rsltActualPrice'));

}

答案 1 :(得分:0)

采用accessor的方式。在模型中,创建一个方法

public function getActualPriceAttribute()
{
    return $this->extend / $this->price;
}

您的模型现在具有您可以调用的“虚拟”属性actualPrice,例如在刀片文件中:

{{ $estates->actual_price }}

有关访问者的更多信息:https://laravel.com/docs/5.7/eloquent-mutators

旁注:$estates = allestates::where('id', $id)->first();可以缩写为$estates = allestates::find($id);

相关问题