gpt4 book ai didi

php - Eloquent - 在 selectRaw() 中插入变量

转载 作者:行者123 更新时间:2023-12-04 08:00:31 29 4
gpt4 key购买 nike

如何在 $TotalDistance 中插入变量( selectRaw() )使用 Eloquent ?

public function totalPace()
{

$TotalDistance = $this->hasMany(Runners_List::class, 'run_list_id')->sum('distance'); //60 karon
$TotalPace = $this->hasMany(Runners_List::class, 'run_list_id')->selectRaw('SEC_TO_TIME( SUM(
TIME_TO_SEC( `pace` ) ) / '$TotalDistance') as total')->first();

return $TotalPace;
}
如果我替换 $TotalDistance 就可以了用一个数字。

最佳答案

尝试这个:

public function totalPace()
{

$TotalDistance = $this->hasMany(Runners_List::class, 'run_list_id')->sum('distance'); //60 karon
$TotalPace = $this->hasMany(Runners_List::class, 'run_list_id')->selectRaw('SEC_TO_TIME( SUM(
TIME_TO_SEC( `pace` ) ) / ?) as total', [$TotalDistance])->first();

return $TotalPace;
}

关于php - Eloquent - 在 selectRaw() 中插入变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66501207/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com