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

huangapple go评论100阅读模式
英文:

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 来获取这些评论:

  1. <div id="videoid">{{$id->id}}</div>
  2. <div id="videotitle">{{$id->title}}</div>
  3. @php($comments = \App\comments::where('video_id', '{{$id->id}}')->get() )
  4. <div id="displaycomment">
  5. @foreach($comments as $comment)
  6. <div id="username">
  7. <div id="con"><h6>{{$comment->id }}</h6></div>
  8. <div id="con"><h6>{{$comment->user_id }}</h6></div>
  9. <div id="con">{{$comment->created_at }}</div>
  10. </div>
  11. <div id="comment">{{$comment->comment }}</div>
  12. @endforeach
  13. </div>

我的控制器运行良好:

  1. public function watch($id)
  2. {
  3. return view('video/watch', compact('id'));
  4. }
英文:

The biggest issue here is how to get these comments using the videoid in my bladeview -- my blade

  1. &lt;div id=&quot;videoid&quot;&gt;{{$id-&gt;id}}&lt;/div&gt;
  2. &lt;div id=&quot;videotitle&quot;&gt;{{$id-&gt;title}}&lt;/div&gt;
  3. @php($comments = \App\comments::where(&#39;video_id&#39;,&#39;{{$id-&gt;id}}&#39;)-&gt;get() )
  4. &lt;div id=&quot;displaycomment&quot;&gt;
  5. @foreach($comments as $comment)
  6. &lt;div id=&quot;username&quot;&gt;
  7. &lt;div id=&quot;con&quot;&gt;&lt;h6&gt;{{$comment-&gt;id }}&lt;/h6&gt;&lt;/div&gt;
  8. &lt;div id=&quot;con&quot;&gt;&lt;h6&gt;{{$comment-&gt;user_id }}&lt;/h6&gt;&lt;/div&gt;
  9. &lt;div id=&quot;con&quot;&gt;{{$comment-&gt;created_at }}&lt;/div&gt;
  10. &lt;/div&gt;
  11. &lt;div id=&quot;comment&quot;&gt;{{$comment-&gt;comment }}&lt;/div&gt;
  12. @endforeach
  13. &lt;/div&gt;

My controller works well --
mycontroller

  1. public function watch($id)
  2. {
  3. return view(&#39;video/watch&#39;, compact(&#39;id&#39;));
  4. }

答案1

得分: 0

  1. public function watch($id)
  2. {
  3. $video = Video::with('comments')->find($id);
  4. $comments = \App\comments::where('video_id', $video->id)->get();
  5. return view('video/watch', compact('video', 'comments'));
  6. }
  1. <div id="videoid">{{$video->id}}</div>
  2. <div id="videotitle">{{$video->title}}</div>
  3. <div id="displaycomment">
  4. @foreach($comments as $comment)
  5. <div id="username">
  6. <div id="con"><h6>{{$comment->id}}</h6></div>
  7. <div id="con"><h6>{{$comment->user_id}}</h6></div>
  8. <div id="con">{{$comment->created_at}}</div>
  9. </div>
  10. <div id="comment">{{$comment->comment}}</div>
  11. @endforeach
  12. </div>
英文:

in controller

  1. public function watch($id)
  2. {
  3. $video = Video::with(&#39;comments&#39;)-&gt;find($id);
  4. $comments = \App\comments::where(&#39;video_id&#39;,&#39;{{$video-&gt;id}}&#39;)-&gt;get()
  5. return view(&#39;video/watch&#39;, compact(&#39;video&#39;,&#39;comments&#39;));
  6. }

in view

  1. &lt;div id=&quot;videoid&quot;&gt;{{$video-&gt;id}}&lt;/div&gt;
  2. &lt;div id=&quot;videotitle&quot;&gt;{{$video-&gt;title}}&lt;/div&gt;
  3. &lt;div id=&quot;displaycomment&quot;&gt;
  4. @foreach($comments as $comment)
  5. &lt;div id=&quot;username&quot;&gt;
  6. &lt;div id=&quot;con&quot;&gt;&lt;h6&gt;{{$comment-&gt;id }}&lt;/h6&gt;&lt;/div&gt;
  7. &lt;div id=&quot;con&quot;&gt;&lt;h6&gt;{{$comment-&gt;user_id }}&lt;/h6&gt;&lt;/div&gt;
  8. &lt;div id=&quot;con&quot;&gt;{{$comment-&gt;created_at }}&lt;/div&gt;
  9. &lt;/div&gt;
  10. &lt;div id=&quot;comment&quot;&gt;{{$comment-&gt;comment }}&lt;/div&gt;
  11. @endforeach
  12. &lt;/div&gt;

答案2

得分: 0

你似乎缺少使用Eloquent的重要部分。

关系

  1. // 视频模型:
  2. public function comments()
  3. {
  4. return $this->hasMany(Comment::class);
  5. }
  6. // 评论模型:
  7. public function video()
  8. {
  9. return $this->belongsTo(Video::class);
  10. }
  11. // 控制器代码:(切换到[路由模型绑定][2])
  12. public function watch(Video $video)
  13. {
  14. return view('video.watch', [
  15. 'video' => $video
  16. ]);
  17. }
  18. // 更新路由以进行路由模型绑定
  19. Route::get('/watch/{video}', 'VideoController@watch')->name('video.watch');
  20. // 视图:
  21. <div id="videoid">{{$video->id}}</div>
  22. <div id="videotitle">{{$video->title}}</div>
  23. <div id="displaycomment">
  24. @foreach ($video->comments as $comment)
  25. <div id="username">
  26. <div id="con">
  27. <h6>{{$comment->id }}</h6>
  28. </div>
  29. <div id="con">
  30. <h6>{{ $comment->user_id }}</h6>
  31. </div>
  32. <div id="con">{{$comment->created_at}}</div>
  33. </div>
  34. <div id="comment">{{$comment->comment}}</div>
  35. @endforeach
  36. </div>

路由模型绑定

英文:

You seem to be missing a key part of using Eloquent.

Relationships.

  1. // Video model:
  2. public function comments()
  3. {
  4. return $this-&gt;hasMany(Comment::class);
  5. }
  6. // Comment model:
  7. public function video()
  8. {
  9. return $this-&gt;belongsTo(Video::class);
  10. }
  11. // Controller code: (Switched to [Route-model binding][2])
  12. public function watch(Video $video)
  13. {
  14. return view(&#39;video.watch&#39;, [
  15. &#39;video&#39; =&gt; $video
  16. ]);
  17. }
  18. // Update routes for Route-model-binding
  19. Route::get(&#39;/watch/{video}&#39;, &#39;VideoController@watch&#39;)-&gt;name(&#39;video.watch&#39;);
  20. // View:
  21. &lt;div id=&quot;videoid&quot;&gt;{{$video-&gt;id}}&lt;/div&gt;
  22. &lt;div id=&quot;videotitle&quot;&gt;{{$video-&gt;title}}&lt;/div&gt;
  23. &lt;div id=&quot;displaycomment&quot;&gt;
  24. @foreach ($video-&gt;comments as $comment)
  25. &lt;div id=&quot;username&quot;&gt;
  26. &lt;div id=&quot;con&quot;&gt;
  27. &lt;h6&gt;{{$comment-&gt;id }}&lt;/h6&gt;
  28. &lt;/div&gt;
  29. &lt;div id=&quot;con&quot;&gt;
  30. &lt;h6&gt;{{ $comment-&gt;user_id }}&lt;/h6&gt;
  31. &lt;/div&gt;
  32. &lt;div id=&quot;con&quot;&gt;$comment-&gt;created_at&lt;/div&gt;
  33. &lt;/div&gt;
  34. &lt;div id=&quot;comment&quot;&gt;$comment-&gt;comment&lt;/div&gt;
  35. @endforeach
  36. &lt;/div&gt;

Route-model binding

答案3

得分: 0

这是你的代码部分,已经翻译好了:

  1. 这是你的 blade 模板。需要从控制器而不是视图中查询。
  2. <div id="videoid">{{ $video->id }}</div>
  3. <div id="videotitle">{{ $video->title }}</div>
  4. <div id="displaycomment">
  5. @foreach($comments as $comment)
  6. <div id="username">
  7. <div id="con"><h6>{{$comment->id }}</h6></div>
  8. <div id="con"><h6>{{$comment->user_id }}</h6></div>
  9. <div id="con">{{$comment->created_at }}</div>
  10. </div>
  11. <div id="comment">{{$comment->comment }}</div>
  12. @endforeach
  13. </div>
  14. 然后,从控制器中获取数据并使用 Laravel 的魔术方法将它们传递到视图中:
  15. public function watch($video_id)
  16. {
  17. $video = Video::whereId($video_id)->first();
  18. $comments = \App\comments::where('video_id',$video_id)->get();
  19. return view('video/watch',[
  20. 'video'=>$video,
  21. 'comments'=>$comments
  22. ]);
  23. }
英文:

This is your blade. Thought there's no need to query from the view but rather from the controller.

  1. &lt;div id=&quot;videoid&quot;&gt;{{ $video-&gt;id }}&lt;/div&gt;
  2. &lt;div id=&quot;videotitle&quot;&gt;{{ $video-&gt;title }}&lt;/div&gt;
  3. &lt;div id=&quot;displaycomment&quot;&gt;
  4. @foreach($comments as $comment)
  5. &lt;div id=&quot;username&quot;&gt;
  6. &lt;div id=&quot;con&quot;&gt;&lt;h6&gt;{{$comment-&gt;id }}&lt;/h6&gt;&lt;/div&gt;
  7. &lt;div id=&quot;con&quot;&gt;&lt;h6&gt;{{$comment-&gt;user_id }}&lt;/h6&gt;&lt;/div&gt;
  8. &lt;div id=&quot;con&quot;&gt;{{$comment-&gt;created_at }}&lt;/div&gt;
  9. &lt;/div&gt;
  10. &lt;div id=&quot;comment&quot;&gt;{{$comment-&gt;comment }}&lt;/div&gt;
  11. @endforeach
  12. &lt;/div&gt;

Then from your controller you can fetch your data and pass them to the view using Laravel's magic method:

  1. public function watch($video_id)
  2. {
  3. $video = Video::whereId($video_id)-&gt;first();
  4. $comments = \App\comments::where(&#39;video_id&#39;,$video_id)-&gt;get()
  5. return view(&#39;video/watch&#39;,[
  6. &#39;video&#39;=&gt;$video,
  7. &#39;comments&#39;=&gt;$comments
  8. ]);
  9. }

huangapple
  • 本文由 发表于 2020年1月6日 18:05:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定