Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Laravel中划分两列?_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 如何在Laravel中划分两列?

Php 如何在Laravel中划分两列?,php,laravel,laravel-5,Php,Laravel,Laravel 5,我正在检索每页每个ID的行,没有问题。DB表中有两列,分别是extend和price。我想对这些列的值使用除法。我的检索代码就在下面。我还想把这段代码添加到除法运算中扩展/价格 public function show($id) { $estates = allestates::where('id', $id)->first(); return view('pages.show', ['estates' => $estates]); } 谢谢你帮助我 您应该尝试以

我正在检索每页每个ID的行,没有问题。DB表中有两列,分别是extend和price。我想对这些列的值使用除法。我的检索代码就在下面。我还想把这段代码添加到除法运算中<代码>扩展/价格

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

    return view('pages.show', ['estates' => $estates]);
}
谢谢你帮助我

您应该尝试以下方法:

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

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

    return view('pages.show', compact('estates','rsltActualPrice'));
}
$rsltActualPrice
此变量放入视图文件中,如下所示:

{{$rsltActualPrice}}

更改视图文件代码,如下所示:

{{$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'));
公共功能显示($id)
{
$estates=allestates::where('id',$id)->first();
打印(“”);
印刷品(不动产);
打印(“”);
出口
$rsltActualPrice=$estates->extend/$estates->price;
返回视图('pages.show',compact('estate','rsltActualPrice');

}按
访问器的方式进行。在模型中,创建一个方法

public function getActualPriceAttribute()
{
    return $this->extend / $this->price;
}
您的模型现在有一个“虚拟”属性
actualPrice
,您可以调用该属性,例如在刀片文件中:

{{$estate->实际价格}

有关访问者的更多信息:


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

请简单地解释一下您的问题,以便理解。
$estate->extend/$estate->price
应该可以。更方便的方法是创建一个。另外,
$estates
有点误导,因为您只得到一个项目(
->first()
)@kerbholz代码没有问题,使用该代码我将每页检索ID的数据。我还对特定列进行了一些计算。@BonishKoirala问题已更新。@Potti:表示您需要在视图文件中使用此结果(即$rsltDivide=extend/price;)?我尝试了第二个选项,因为我认为这是一种简单的方法<代码>{{$estates->extend/$estates->price}
但是我得到了这个错误
遇到一个格式不正确的数值(View:/var/www/html/laravel/resources/views/pages/show.blade.php)
我也试图使用这个函数,但出现了这个错误:
遇到一个格式不正确的数值
@Potti:请显示您的结果(即打印(“”);打印($estate);打印(“”);退出;)还可以这样添加:$rsltActualPrice=(int)$estates->extend/(int)$estates->price;好的@JinalSomaiya解决了这个问题!非常感谢。它返回这个错误,伙计
遇到格式不正确的数值(View:/var/www/html/laravel/resources/views/pages/show.blade.php)
您需要确保您的值实际上是可除的。这里的所有答案/建议都假设它们已经存在。谢谢您的帮助。我想问题是:我有很多行,有些行不是数字而是字符串。但我怎样才能克服这个问题呢?我只想划分哪些行有数字听起来像是数据库的“问题”。确保这些字段中只有“数字”值。如果要对数据库中的字段进行计算,则该字段不应为字符串。也许值得一提新问题