php - 在 Laravel 中通过表格获得一行的最佳方法是什么?

我的数据库中有以下表格:

我想列出评论评分最高的视频,如下所示:

app/Helpers/function.php

function best_comment($data, $value = 'name') {
    $properties = array();

    foreach($data->episodes as $episode) {
        if(! isset($properties['id']) || $properties['id'] > $season->bestComment[0]->id) {
            $properties['id'] = $season->bestComment[0]->id;
            $properties['name'] = $season->bestComment[0]->name;
        }
    }
    
    if(! empty($properties)) {
        return $properties[$value];
    }
}

app/Video.php

public function episodes() {
    return $this->hasMany(Episode::class);
}

app/Episode.php

public function bestComment() {
    return $this->belongsToMany(Rating::class, 'comments', 'id')->orderBy('id', 'asc')->limit(1);
}

app/Http/Controllers/VideosController.php

public function index() {
    $videos = Videos::query();

    $videos->with('episodes');

    return view('videos.index')->with('videos', $videos->paginate(4));
}

resources/views/videos/index.blade.php

@foreach($videos as $video)
    <div>
        <h5>{{ $video->title }}</h5>
        Best comment rating: {{ best_comment($video, 'name') }}
    </div>
@endforeach

我的解决方案有很多疑问。打印评分最高的评论的最佳方式是什么?

Laravel Sandbox

最佳答案

这不是最佳解决方案,但应该可行。

首先从分页对象中获取 Video 模型的 ID:

$ids = $videos->getCollection()->pluck('id');

然后使用这些加入其他表格,按视频分组并选择评分最高的 ID。如果您的评分表上有其他列,您可能需要对此进行调整,也就是说,这样您可以比使用它们的主键更好地对它们进行排名。

$videoToRatingMap = Videos::query()
    ->whereIn('id', $ids)
    ->join('episodes', 'episodes.video_id', '=', 'videos.id')
    ->join('comments', 'comments.episode_id ', '=', 'episodes.id')
    ->join('ratings', 'ratings.id', '=', 'comments.rating_id')
    ->groupBy('videos.id')
    ->select('videos.id')
    ->selectRaw('max(ratings.id) as rating_id')
    ->get();

这将为您提供按 ID 和排名最高的评论显示的视频 map 。

然而,这不会让您获得排名的名称,这可以在单独的查询中完成:

$ratingNames = Rating::query()
    ->findMany($videoToRatingMap->pluck('rating_id'))
    ->pluck('name', 'id');

然后转换评级图以包含名称:

$videoToRatingMap->transform(function($video) use ($ratingNames){
    $video['rating_name'] = $ratingNames[$video['rating_id']];
    return $video;
})->keyBy('id');

这将为您提供一个集合,以视频 ID 为键,其中还包含评级 ID 及其名称。

执行此操作的一种稍微更优化的方法是采用第一个查询并将整个查询包装到一个子查询中,这样您就可以再次加入评级表并获取名称。将以使单个查询更复杂为代价将查询减少一个。

https://stackoverflow.com/questions/63288336/

相关文章:

.net-core - 如何停止 blazor @onclick 重新渲染页面

c++ - 使用 CMake 与网络构建 Qt 项目导致 undefined reference

azure - 从 TFS 2013 到 Azure Devops 服务的源代码迁移

angular - ionic /Angular 将对象推送到数组对象

python - 当某些列值为空时,如何合并 Dataframe 中的多行?

angular - 如何更新 Angular 中提供者提供的值?

c# - Microsoft.AspNetCore.Server.IIS.Core.IISHttpC

c# - EpiServer DynamicDataStore LINQ 语句中的 sql 语法不正

node.js - cors 问题与 passport.js google oauth 策略

shell - shell脚本的Groovy参数