我想为每篇帖子显示评论。

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

I want to displaying comments for each post

问题

以下是您要翻译的代码部分:

  1. public function index() {
  2. $posts = Post::all();
  3. $comment = Comment::all();
  4. return view('home', [
  5. 'posts' => $posts,
  6. 'comments' => $comment
  7. ]);
  8. }
  1. public function comments() {
  2. return $this->hasMany(Comment::class);
  3. }
  4. public function user() {
  5. return $this->belongsTo(User::class);
  6. }
  1. public function post() {
  2. return $this->belongsTo(Post::class);
  3. }
  4. public function user() {
  5. return $this->belongsTo(User::class);
  6. }
  1. public function post() {
  2. return $this->hasMany(Post::class);
  3. }
  1. @foreach ($posts as $post)
  2. <div class="box-post">
  3. <p>{{ $post->user->name }}</p>
  4. <p>{{ $post->post_content}}</p>
  5. <a href="/comment/{{ $post->id }}"><button>Comment</button></a>
  6. </div>
  7. @foreach ($comments as $comment)
  8. <div class="box-post">{{ $comment->comment_content }}</div>
  9. @endforeach
  10. @endforeach

希望这些翻译对您有所帮助。如果您有任何其他问题,请随时提问。

英文:

hello I just moved from php native and want to try relationships in laravel, so I want to display a post and each comment and the name of user who comments so that when looped the post has a comment that has a relationship with it.

So this is my HomeController.php :

  1. public function index() {
  2. $posts = Post::all();
  3. $comment = Comment::all();
  4. return view(&#39;home&#39;, [
  5. &#39;posts&#39; =&gt; $posts,
  6. &#39;comments&#39; =&gt; $comment
  7. ]);
  8. }

My Post.php :

  1. public function comments() {
  2. return $this-&gt;hasMany(Comment::class);
  3. }
  4. public function user() {
  5. return $this-&gt;belongsTo(User::class);
  6. }

My Comments.php :

  1. public function post() {
  2. return $this-&gt;belongsTo(Post::class);
  3. }
  4. public function user() {
  5. return $this-&gt;belongsTo(User::class);
  6. }

My User.php :

  1. public function post() {
  2. return $this-&gt;hasMany(Post::class);
  3. }

My home.blade.php :

  1. @foreach ($posts as $post)
  2. &lt;div class=&quot;box-post&quot;&gt;
  3. &lt;p&gt;{{ $post-&gt;user-&gt;name }}&lt;/p&gt;
  4. &lt;p&gt;{{ $post-&gt;post_content}}&lt;/p&gt;
  5. &lt;a href=&quot;/comment/{{ $post-&gt;id }}&quot;&gt;&lt;button&gt;Comment&lt;/button&gt;&lt;/a&gt;
  6. &lt;/div&gt;
  7. @foreach ($comments as $comment)
  8. &lt;div class=&quot;box-post&quot;&gt;{{ $comment-&gt;comment_content }} &lt;/div&gt;
  9. @endforeach
  10. @endforeach

the code above runs with the post displayed along with the author but all the comments also appear in all posts, I have searched for references and tried to change the comments value in Controller but the results are still the same so I made the comments appear in each post *$comment = Comment::all();.

what I want is to display posts and comments that relate to each post, like a twitter feature that can reply to people's tweets.

Thx you..

答案1

得分: 0

  1. public function index() {
  2. $posts = Post::with('comments')->get();
  3. return view('home', [
  4. 'posts' => $posts,
  5. ]);
  6. }

In Blade:

  1. @foreach ($posts as $post)
  2. <div class="box-post">
  3. <p>{{ $post->user->name }}</p>
  4. <p>{{ $post->post_content}}</p>
  5. <a href="/comment/{{ $post->id }}"><button>Comment</button></a>
  6. </div>
  7. @foreach ($post->comments as $comment)
  8. <div class="box-post">{{ $comment->comment_content }}</div>
  9. @endforeach
  10. @endforeach
英文:
  1. public function index() {
  2. $posts = Post::with(&#39;comments&#39;)-&gt;get();
  3. return view(&#39;home&#39;, [
  4. &#39;posts&#39; =&gt; $posts,
  5. ]);
  6. }

then in blade

  1. @foreach ($posts as $post)
  2. &lt;div class=&quot;box-post&quot;&gt;
  3. &lt;p&gt;{{ $post-&gt;user-&gt;name }}&lt;/p&gt;
  4. &lt;p&gt;{{ $post-&gt;post_content}}&lt;/p&gt;
  5. &lt;a href=&quot;/comment/{{ $post-&gt;id }}&quot;&gt;&lt;button&gt;Comment&lt;/button&gt;&lt;/a&gt;
  6. &lt;/div&gt;
  7. @foreach ($post-&gt;comments as $comment)
  8. &lt;div class=&quot;box-post&quot;&gt;{{ $comment-&gt;comment_content }} &lt;/div&gt;
  9. @endforeach
  10. @endforeach

答案2

得分: 0

你的关系建立得很好,因此只需简单使用$post->comments即可获取与每篇帖子相关的所有评论。

  1. @foreach ($posts as $post)
  2. <div class="box-post">
  3. <p>{{ $post->user->name }}</p>
  4. <p>{{ $post->post_content}}</p>
  5. <a href="/comment/{{ $post->id }}"><button>Comment</button></a>
  6. </div>
  7. @foreach ($post->comments as $comment) // 在这里进行更改
  8. <div class="box-post">{{ $comment->comment_content }} </div>
  9. @endforeach
  10. @endforeach
英文:

Your relations builded well, so just can simply use $post-&gt;comments can get all comments related on each post.

  1. @foreach ($posts as $post)
  2. &lt;div class=&quot;box-post&quot;&gt;
  3. &lt;p&gt;{{ $post-&gt;user-&gt;name }}&lt;/p&gt;
  4. &lt;p&gt;{{ $post-&gt;post_content}}&lt;/p&gt;
  5. &lt;a href=&quot;/comment/{{ $post-&gt;id }}&quot;&gt;&lt;button&gt;Comment&lt;/button&gt;&lt;/a&gt;
  6. &lt;/div&gt;
  7. @foreach ($post-&gt;comments as $comment) // make changes here
  8. &lt;div class=&quot;box-post&quot;&gt;{{ $comment-&gt;comment_content }} &lt;/div&gt;
  9. @endforeach
  10. @endforeach

答案3

得分: 0

你在构建模型及其关系方面做得很出色。

我建议你更改调用帖子查询的方式。

  1. public function index(Request $request) {
  2. // 为每个帖子和用户添加及时加载评论
  3. $posts = Post::with('comments', 'user')->get();
  4. // 需要及时加载用户,因为在Blade模板中你调用了 $post->user->name
  5. // 不再需要调用评论
  6. // 仅返回 $posts
  7. return view('home', compact('posts'));
  8. }

在你的Blade模板中,你可以这样调用每个帖子的评论。

  1. @foreach ($posts as $post)
  2. <div class="box-post">
  3. <p>{{ $post->user->name }}</p>
  4. <p>{{ $post->post_content }}</p>
  5. <a href="/comment/{{ $post->id }}"><button>Comment</button></a>
  6. </div>
  7. @foreach ($post->comments as $comment)
  8. <div class="box-post">{{ $comment->comment_content }}</div>
  9. @endforeach
  10. @endforeach

就是这样,如果你使用 barryvdh/laravel-debugbar,你会看到只运行了3个查询,每个查询都来自帖子、评论和用户表。对于评论查询和用户查询,不会获取所有行,只会获取与第一个查询中获取的帖子相关的行。

使用Laravel框架时玩得开心!你的PHP本地知识和专业知识将非常有用,可以加速你的学习和掌握过程。

英文:

You have done fine job building models and their relationship.

I recommend you change how you call post query.

  1. public function index(Request $request) {
  2. // add eager loading comments for each post and user
  3. $posts = Post::with(&#39;comments&#39;, &#39;user&#39;)-&gt;get();
  4. // eager loading user is needed, cause in blade you call $post-&gt;user-&gt;name
  5. // calling comments will no longer needed
  6. // return only $posts
  7. return view(&#39;home&#39;, compact(&#39;posts&#39;));
  8. }

And inside your blade, you can call comments from each post, like this.

  1. @foreach ($posts as $post)
  2. &lt;div class=&quot;box-post&quot;&gt;
  3. &lt;p&gt;{{ $post-&gt;user-&gt;name }}&lt;/p&gt;
  4. &lt;p&gt;{{ $post-&gt;post_content}}&lt;/p&gt;
  5. &lt;a href=&quot;/comment/{{ $post-&gt;id }}&quot;&gt;&lt;button&gt;Comment&lt;/button&gt;&lt;/a&gt;
  6. &lt;/div&gt;
  7. @foreach ($post-&gt;comments as $comment)
  8. &lt;div class=&quot;box-post&quot;&gt;{{ $comment-&gt;comment_content }} &lt;/div&gt;
  9. @endforeach
  10. @endforeach

That's it and if you use barryvdh/laravel-debugbar , you will see there are only 3 queries run, each one from table posts, comments and users.<br>
And for comments query and users query, not all rows will be fetched,<br>
only related to fetched posts from the 1st query.

Have fun using Laravel Framework!<br>
Your PHP native knowledge/expertise will be very useful and speed up your learning/mastering process.

huangapple
  • 本文由 发表于 2023年2月10日 11:13:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406591.html
匿名

发表评论

匿名网友

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

确定