英文:
i want to get a video id so that it can help me get comments related to that video in my site, it only displays a video but not comments
问题
以下是翻译好的部分:
问题的关键在于如何在我的 blade 视图中使用 videoid 来获取这些评论:
<div id="videoid">{{$id->id}}</div>
<div id="videotitle">{{$id->title}}</div>
@php($comments = \App\comments::where('video_id', '{{$id->id}}')->get() )
<div id="displaycomment">
@foreach($comments as $comment)
<div id="username">
<div id="con"><h6>{{$comment->id }}</h6></div>
<div id="con"><h6>{{$comment->user_id }}</h6></div>
<div id="con">{{$comment->created_at }}</div>
</div>
<div id="comment">{{$comment->comment }}</div>
@endforeach
</div>
我的控制器运行良好:
public function watch($id)
{
return view('video/watch', compact('id'));
}
英文:
The biggest issue here is how to get these comments using the videoid in my bladeview -- my blade
<div id="videoid">{{$id->id}}</div>
<div id="videotitle">{{$id->title}}</div>
@php($comments = \App\comments::where('video_id','{{$id->id}}')->get() )
<div id="displaycomment">
@foreach($comments as $comment)
<div id="username">
<div id="con"><h6>{{$comment->id }}</h6></div>
<div id="con"><h6>{{$comment->user_id }}</h6></div>
<div id="con">{{$comment->created_at }}</div>
</div>
<div id="comment">{{$comment->comment }}</div>
@endforeach
</div>
My controller works well --
mycontroller
public function watch($id)
{
return view('video/watch', compact('id'));
}
答案1
得分: 0
public function watch($id)
{
$video = Video::with('comments')->find($id);
$comments = \App\comments::where('video_id', $video->id)->get();
return view('video/watch', compact('video', 'comments'));
}
<div id="videoid">{{$video->id}}</div>
<div id="videotitle">{{$video->title}}</div>
<div id="displaycomment">
@foreach($comments as $comment)
<div id="username">
<div id="con"><h6>{{$comment->id}}</h6></div>
<div id="con"><h6>{{$comment->user_id}}</h6></div>
<div id="con">{{$comment->created_at}}</div>
</div>
<div id="comment">{{$comment->comment}}</div>
@endforeach
</div>
英文:
in controller
public function watch($id)
{
$video = Video::with('comments')->find($id);
$comments = \App\comments::where('video_id','{{$video->id}}')->get()
return view('video/watch', compact('video','comments'));
}
in view
<div id="videoid">{{$video->id}}</div>
<div id="videotitle">{{$video->title}}</div>
<div id="displaycomment">
@foreach($comments as $comment)
<div id="username">
<div id="con"><h6>{{$comment->id }}</h6></div>
<div id="con"><h6>{{$comment->user_id }}</h6></div>
<div id="con">{{$comment->created_at }}</div>
</div>
<div id="comment">{{$comment->comment }}</div>
@endforeach
</div>
答案2
得分: 0
你似乎缺少使用Eloquent的重要部分。
关系。
// 视频模型:
public function comments()
{
return $this->hasMany(Comment::class);
}
// 评论模型:
public function video()
{
return $this->belongsTo(Video::class);
}
// 控制器代码:(切换到[路由模型绑定][2])
public function watch(Video $video)
{
return view('video.watch', [
'video' => $video
]);
}
// 更新路由以进行路由模型绑定
Route::get('/watch/{video}', 'VideoController@watch')->name('video.watch');
// 视图:
<div id="videoid">{{$video->id}}</div>
<div id="videotitle">{{$video->title}}</div>
<div id="displaycomment">
@foreach ($video->comments as $comment)
<div id="username">
<div id="con">
<h6>{{$comment->id }}</h6>
</div>
<div id="con">
<h6>{{ $comment->user_id }}</h6>
</div>
<div id="con">{{$comment->created_at}}</div>
</div>
<div id="comment">{{$comment->comment}}</div>
@endforeach
</div>
英文:
You seem to be missing a key part of using Eloquent.
// Video model:
public function comments()
{
return $this->hasMany(Comment::class);
}
// Comment model:
public function video()
{
return $this->belongsTo(Video::class);
}
// Controller code: (Switched to [Route-model binding][2])
public function watch(Video $video)
{
return view('video.watch', [
'video' => $video
]);
}
// Update routes for Route-model-binding
Route::get('/watch/{video}', 'VideoController@watch')->name('video.watch');
// View:
<div id="videoid">{{$video->id}}</div>
<div id="videotitle">{{$video->title}}</div>
<div id="displaycomment">
@foreach ($video->comments as $comment)
<div id="username">
<div id="con">
<h6>{{$comment->id }}</h6>
</div>
<div id="con">
<h6>{{ $comment->user_id }}</h6>
</div>
<div id="con">$comment->created_at</div>
</div>
<div id="comment">$comment->comment</div>
@endforeach
</div>
答案3
得分: 0
这是你的代码部分,已经翻译好了:
这是你的 blade 模板。需要从控制器而不是视图中查询。
<div id="videoid">{{ $video->id }}</div>
<div id="videotitle">{{ $video->title }}</div>
<div id="displaycomment">
@foreach($comments as $comment)
<div id="username">
<div id="con"><h6>{{$comment->id }}</h6></div>
<div id="con"><h6>{{$comment->user_id }}</h6></div>
<div id="con">{{$comment->created_at }}</div>
</div>
<div id="comment">{{$comment->comment }}</div>
@endforeach
</div>
然后,从控制器中获取数据并使用 Laravel 的魔术方法将它们传递到视图中:
public function watch($video_id)
{
$video = Video::whereId($video_id)->first();
$comments = \App\comments::where('video_id',$video_id)->get();
return view('video/watch',[
'video'=>$video,
'comments'=>$comments
]);
}
英文:
This is your blade. Thought there's no need to query from the view but rather from the controller.
<div id="videoid">{{ $video->id }}</div>
<div id="videotitle">{{ $video->title }}</div>
<div id="displaycomment">
@foreach($comments as $comment)
<div id="username">
<div id="con"><h6>{{$comment->id }}</h6></div>
<div id="con"><h6>{{$comment->user_id }}</h6></div>
<div id="con">{{$comment->created_at }}</div>
</div>
<div id="comment">{{$comment->comment }}</div>
@endforeach
</div>
Then from your controller you can fetch your data and pass them to the view using Laravel's magic method:
public function watch($video_id)
{
$video = Video::whereId($video_id)->first();
$comments = \App\comments::where('video_id',$video_id)->get()
return view('video/watch',[
'video'=>$video,
'comments'=>$comments
]);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论