英文:
I want to displaying comments for each post
问题
以下是您要翻译的代码部分:
public function index() {
$posts = Post::all();
$comment = Comment::all();
return view('home', [
'posts' => $posts,
'comments' => $comment
]);
}
public function comments() {
return $this->hasMany(Comment::class);
}
public function user() {
return $this->belongsTo(User::class);
}
public function post() {
return $this->belongsTo(Post::class);
}
public function user() {
return $this->belongsTo(User::class);
}
public function post() {
return $this->hasMany(Post::class);
}
@foreach ($posts as $post)
<div class="box-post">
<p>{{ $post->user->name }}</p>
<p>{{ $post->post_content}}</p>
<a href="/comment/{{ $post->id }}"><button>Comment</button></a>
</div>
@foreach ($comments as $comment)
<div class="box-post">{{ $comment->comment_content }}</div>
@endforeach
@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 :
public function index() {
$posts = Post::all();
$comment = Comment::all();
return view('home', [
'posts' => $posts,
'comments' => $comment
]);
}
My Post.php :
public function comments() {
return $this->hasMany(Comment::class);
}
public function user() {
return $this->belongsTo(User::class);
}
My Comments.php :
public function post() {
return $this->belongsTo(Post::class);
}
public function user() {
return $this->belongsTo(User::class);
}
My User.php :
public function post() {
return $this->hasMany(Post::class);
}
My home.blade.php :
@foreach ($posts as $post)
<div class="box-post">
<p>{{ $post->user->name }}</p>
<p>{{ $post->post_content}}</p>
<a href="/comment/{{ $post->id }}"><button>Comment</button></a>
</div>
@foreach ($comments as $comment)
<div class="box-post">{{ $comment->comment_content }} </div>
@endforeach
@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
public function index() {
$posts = Post::with('comments')->get();
return view('home', [
'posts' => $posts,
]);
}
In Blade:
@foreach ($posts as $post)
<div class="box-post">
<p>{{ $post->user->name }}</p>
<p>{{ $post->post_content}}</p>
<a href="/comment/{{ $post->id }}"><button>Comment</button></a>
</div>
@foreach ($post->comments as $comment)
<div class="box-post">{{ $comment->comment_content }}</div>
@endforeach
@endforeach
英文:
public function index() {
$posts = Post::with('comments')->get();
return view('home', [
'posts' => $posts,
]);
}
then in blade
@foreach ($posts as $post)
<div class="box-post">
<p>{{ $post->user->name }}</p>
<p>{{ $post->post_content}}</p>
<a href="/comment/{{ $post->id }}"><button>Comment</button></a>
</div>
@foreach ($post->comments as $comment)
<div class="box-post">{{ $comment->comment_content }} </div>
@endforeach
@endforeach
答案2
得分: 0
你的关系建立得很好,因此只需简单使用$post->comments
即可获取与每篇帖子相关的所有评论。
@foreach ($posts as $post)
<div class="box-post">
<p>{{ $post->user->name }}</p>
<p>{{ $post->post_content}}</p>
<a href="/comment/{{ $post->id }}"><button>Comment</button></a>
</div>
@foreach ($post->comments as $comment) // 在这里进行更改
<div class="box-post">{{ $comment->comment_content }} </div>
@endforeach
@endforeach
英文:
Your relations builded well, so just can simply use $post->comments
can get all comments related on each post.
@foreach ($posts as $post)
<div class="box-post">
<p>{{ $post->user->name }}</p>
<p>{{ $post->post_content}}</p>
<a href="/comment/{{ $post->id }}"><button>Comment</button></a>
</div>
@foreach ($post->comments as $comment) // make changes here
<div class="box-post">{{ $comment->comment_content }} </div>
@endforeach
@endforeach
答案3
得分: 0
你在构建模型及其关系方面做得很出色。
我建议你更改调用帖子查询的方式。
public function index(Request $request) {
// 为每个帖子和用户添加及时加载评论
$posts = Post::with('comments', 'user')->get();
// 需要及时加载用户,因为在Blade模板中你调用了 $post->user->name
// 不再需要调用评论
// 仅返回 $posts
return view('home', compact('posts'));
}
在你的Blade模板中,你可以这样调用每个帖子的评论。
@foreach ($posts as $post)
<div class="box-post">
<p>{{ $post->user->name }}</p>
<p>{{ $post->post_content }}</p>
<a href="/comment/{{ $post->id }}"><button>Comment</button></a>
</div>
@foreach ($post->comments as $comment)
<div class="box-post">{{ $comment->comment_content }}</div>
@endforeach
@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.
public function index(Request $request) {
// add eager loading comments for each post and user
$posts = Post::with('comments', 'user')->get();
// eager loading user is needed, cause in blade you call $post->user->name
// calling comments will no longer needed
// return only $posts
return view('home', compact('posts'));
}
And inside your blade, you can call comments from each post, like this.
@foreach ($posts as $post)
<div class="box-post">
<p>{{ $post->user->name }}</p>
<p>{{ $post->post_content}}</p>
<a href="/comment/{{ $post->id }}"><button>Comment</button></a>
</div>
@foreach ($post->comments as $comment)
<div class="box-post">{{ $comment->comment_content }} </div>
@endforeach
@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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论