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评论61阅读模式
英文:

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

&lt;div id=&quot;videoid&quot;&gt;{{$id-&gt;id}}&lt;/div&gt;
&lt;div id=&quot;videotitle&quot;&gt;{{$id-&gt;title}}&lt;/div&gt;
@php($comments = \App\comments::where(&#39;video_id&#39;,&#39;{{$id-&gt;id}}&#39;)-&gt;get() )
&lt;div id=&quot;displaycomment&quot;&gt;
@foreach($comments as $comment)
&lt;div id=&quot;username&quot;&gt;
    &lt;div id=&quot;con&quot;&gt;&lt;h6&gt;{{$comment-&gt;id }}&lt;/h6&gt;&lt;/div&gt;
    &lt;div id=&quot;con&quot;&gt;&lt;h6&gt;{{$comment-&gt;user_id }}&lt;/h6&gt;&lt;/div&gt;
    &lt;div id=&quot;con&quot;&gt;{{$comment-&gt;created_at }}&lt;/div&gt;
    &lt;/div&gt;
    &lt;div id=&quot;comment&quot;&gt;{{$comment-&gt;comment }}&lt;/div&gt;
 @endforeach
&lt;/div&gt;

My controller works well --
mycontroller

 public function watch($id)
{
    return view(&#39;video/watch&#39;, compact(&#39;id&#39;));
}

答案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(&#39;comments&#39;)-&gt;find($id);
    $comments = \App\comments::where(&#39;video_id&#39;,&#39;{{$video-&gt;id}}&#39;)-&gt;get()
    return view(&#39;video/watch&#39;, compact(&#39;video&#39;,&#39;comments&#39;));
}

in view

&lt;div id=&quot;videoid&quot;&gt;{{$video-&gt;id}}&lt;/div&gt;
&lt;div id=&quot;videotitle&quot;&gt;{{$video-&gt;title}}&lt;/div&gt;

&lt;div id=&quot;displaycomment&quot;&gt;
@foreach($comments as $comment)
&lt;div id=&quot;username&quot;&gt;
    &lt;div id=&quot;con&quot;&gt;&lt;h6&gt;{{$comment-&gt;id }}&lt;/h6&gt;&lt;/div&gt;
    &lt;div id=&quot;con&quot;&gt;&lt;h6&gt;{{$comment-&gt;user_id }}&lt;/h6&gt;&lt;/div&gt;
    &lt;div id=&quot;con&quot;&gt;{{$comment-&gt;created_at }}&lt;/div&gt;
    &lt;/div&gt;
    &lt;div id=&quot;comment&quot;&gt;{{$comment-&gt;comment }}&lt;/div&gt;
 @endforeach
&lt;/div&gt;

答案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.

Relationships.

// Video model:
public function comments()
{
    return $this-&gt;hasMany(Comment::class);
}

// Comment model:
public function video()
{
    return $this-&gt;belongsTo(Video::class);
}

// Controller code: (Switched to [Route-model binding][2])
public function watch(Video $video)
{
    return view(&#39;video.watch&#39;, [
        &#39;video&#39; =&gt; $video
    ]);
}

// Update routes for Route-model-binding
Route::get(&#39;/watch/{video}&#39;, &#39;VideoController@watch&#39;)-&gt;name(&#39;video.watch&#39;);

// View:
&lt;div id=&quot;videoid&quot;&gt;{{$video-&gt;id}}&lt;/div&gt;
&lt;div id=&quot;videotitle&quot;&gt;{{$video-&gt;title}}&lt;/div&gt;

&lt;div id=&quot;displaycomment&quot;&gt;
    @foreach ($video-&gt;comments as $comment)
    &lt;div id=&quot;username&quot;&gt;
        &lt;div id=&quot;con&quot;&gt;
            &lt;h6&gt;{{$comment-&gt;id }}&lt;/h6&gt;
        &lt;/div&gt;
        &lt;div id=&quot;con&quot;&gt;
            &lt;h6&gt;{{ $comment-&gt;user_id }}&lt;/h6&gt;
        &lt;/div&gt;
        &lt;div id=&quot;con&quot;&gt;$comment-&gt;created_at&lt;/div&gt;
    &lt;/div&gt;
    &lt;div id=&quot;comment&quot;&gt;$comment-&gt;comment&lt;/div&gt;
    @endforeach
&lt;/div&gt;

Route-model binding

答案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.

 &lt;div id=&quot;videoid&quot;&gt;{{ $video-&gt;id }}&lt;/div&gt;
&lt;div id=&quot;videotitle&quot;&gt;{{ $video-&gt;title }}&lt;/div&gt;
&lt;div id=&quot;displaycomment&quot;&gt;
@foreach($comments as $comment)
&lt;div id=&quot;username&quot;&gt;
&lt;div id=&quot;con&quot;&gt;&lt;h6&gt;{{$comment-&gt;id }}&lt;/h6&gt;&lt;/div&gt;
&lt;div id=&quot;con&quot;&gt;&lt;h6&gt;{{$comment-&gt;user_id }}&lt;/h6&gt;&lt;/div&gt;
&lt;div id=&quot;con&quot;&gt;{{$comment-&gt;created_at }}&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&quot;comment&quot;&gt;{{$comment-&gt;comment }}&lt;/div&gt;
@endforeach
&lt;/div&gt;

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)-&gt;first();
$comments = \App\comments::where(&#39;video_id&#39;,$video_id)-&gt;get()
return view(&#39;video/watch&#39;,[
&#39;video&#39;=&gt;$video,
&#39;comments&#39;=&gt;$comments
]);
}

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:

确定