How can you insert a variable ($TotalDistance
) inside a selectRaw()
using 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;
}
It works if I replace $TotalDistance
with a number.
My Answer:
>selectRaw("SUM(distance) as total_distance")->value('total_distance');
$totalPace = $this->selectRaw("SUM(time) / $TotalDistance as total_pace")->value('total_pace');
return $totalPace;
}
Rate this post
5 of 5 based on 8812 votesComments