John Davidson

php - Eloquent - Inserting variable inside selectRaw()

0 comments
Message:


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 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us