Files
mtdb_movie/app/Actions/Reviews/UpdateReviewableAverageScore.php
maher 703f50a09d
Some checks failed
Build / run (push) Has been cancelled
first commit
2025-10-29 11:42:25 +01:00

33 lines
850 B
PHP
Executable File

<?php
namespace App\Actions\Reviews;
use App\Models\Review;
use Illuminate\Support\Facades\DB;
class UpdateReviewableAverageScore
{
public function execute(int $reviewableId, string $reviewableType): void
{
$votes = app(Review::class)
->where('reviewable_type', $reviewableType)
->where('reviewable_id', $reviewableId)
->select(
DB::raw('avg(`score`) as average'),
DB::raw('count(*) as count'),
)
->first();
$average = number_format((float) $votes['average'], 1);
// title or episode
$model = app(modelTypeToNamespace($reviewableType))->find(
$reviewableId,
);
$model->local_vote_average = $average;
$model->local_vote_count = $votes['count'];
$model->save();
}
}