【发布时间】:2020-11-26 23:55:21
【问题描述】:
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));
}
资源/视图/视频/index.blade.php
@foreach($videos as $video)
<div>
<h5>{{ $video->title }}</h5>
Best comment rating: {{ best_comment($video, 'name') }}
</div>
@endforeach
我的解决方案有很多疑问。打印评分最高的评论的最佳方式是什么?
【问题讨论】:
-
不要在问题上链接图片,将它们直接注入问题中
-
请发布视频模型...并且,请通知我这里发表评论的 chenges,否则我将无法知道您已发布
-
我已经扩展了描述。