获取Laravel中文章id的用户名和评论
Get the user name and comment with article id in Laravel
我有 3 table 的用户、评论和文章。我有这样的路线:
Route::get('/article/{id}', 'ArticlesController@view');
我想要的是当我访问该路由时,我将在本文 ID 中获得用户名和他们的评论。
所以这是我在 ArticlesController 中的视图函数:
public function view($id){
$article = Article::with('comments')->find($id);
return view('front.single',compact('article'));
}
这是我的 single.blade.php 代码:
<div class="single-grid">
@section('content')
<h1>{{ $article->title }}</h1>
<p>{{ $article->content }}</p>
@show
</div>
<div class="comment-list">
@foreach($article->comments as $comment)
<ul class="comment-list">
<div class="comment-grid">
<img src="/images/avatar.png" alt=""/>
<div class="comment-info">
<h4>{{ $comment->user->name }}</h4>
<p>{{ $comment->comment }}</p>
<h5>{{ $comment->created_at->diffForHumans() }}</h5>
<a href="#">Reply</a>
</div>
<div class="clearfix"></div>
</div>
</ul>
@endforeach
</div>
我不知道该怎么做,因为它给我这样的错误:
"Call to undefined relationship [comment] on model [App\User]."
我已经在每个模型中定义了关系。这是我的文章模型:
public function comments(){
return $this->hasMany(Comment::class);
}
public function user(){
return $this->belongsTo(User::class);
}
我的评论模型:
public function article(){
$this->belongsTo(Article::class);
}
public function user(){
$this->belongsTo(User::class);
}
这是我的用户模型:
public function articles(){
return $this->hasMany(Article::class);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
public function publish(Article $article){
$this->articles()->save($article);
}
这是我的 table 结构:
-users(id,name,email,password,remember_token,created_at,updated_at)
-评论(id,user_id,article_id,评论,created_at,updated_at)
-文章(id,user_id,标题,内容,created_at,updated_at)
那么我怎样才能通过使用这条路线获得用户名呢?谢谢。
在您的评论模型上,您需要将文章替换为文章
public function article(){
$this->belongsTo(Article::class);
}
此外,如果您想获得用户特定的评论,则需要更改您的控制器操作代码
$article = Article::with('user.comment')->find($id) to
$article = Article::with('user.comments')->find($id);
我认为您的问题来自于使用 compact 函数:数组被传递到视图而不是对象。
你能这样试试吗:
// Controller
public function view($id) {
$article = Article::findOrFail($id);
return view('front.single', $article);
}
<!-- View -->
@foreach($article->comments as $comment)
{{ $comment->user->name }}
{{ $comment->comment }}
{{ $comment->created_at->diffForHumans() }}
@endforeach
我有 3 table 的用户、评论和文章。我有这样的路线:
Route::get('/article/{id}', 'ArticlesController@view');
我想要的是当我访问该路由时,我将在本文 ID 中获得用户名和他们的评论。
所以这是我在 ArticlesController 中的视图函数:
public function view($id){
$article = Article::with('comments')->find($id);
return view('front.single',compact('article'));
}
这是我的 single.blade.php 代码:
<div class="single-grid">
@section('content')
<h1>{{ $article->title }}</h1>
<p>{{ $article->content }}</p>
@show
</div>
<div class="comment-list">
@foreach($article->comments as $comment)
<ul class="comment-list">
<div class="comment-grid">
<img src="/images/avatar.png" alt=""/>
<div class="comment-info">
<h4>{{ $comment->user->name }}</h4>
<p>{{ $comment->comment }}</p>
<h5>{{ $comment->created_at->diffForHumans() }}</h5>
<a href="#">Reply</a>
</div>
<div class="clearfix"></div>
</div>
</ul>
@endforeach
</div>
我不知道该怎么做,因为它给我这样的错误:
"Call to undefined relationship [comment] on model [App\User]."
我已经在每个模型中定义了关系。这是我的文章模型:
public function comments(){
return $this->hasMany(Comment::class);
}
public function user(){
return $this->belongsTo(User::class);
}
我的评论模型:
public function article(){
$this->belongsTo(Article::class);
}
public function user(){
$this->belongsTo(User::class);
}
这是我的用户模型:
public function articles(){
return $this->hasMany(Article::class);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
public function publish(Article $article){
$this->articles()->save($article);
}
这是我的 table 结构: -users(id,name,email,password,remember_token,created_at,updated_at) -评论(id,user_id,article_id,评论,created_at,updated_at) -文章(id,user_id,标题,内容,created_at,updated_at)
那么我怎样才能通过使用这条路线获得用户名呢?谢谢。
在您的评论模型上,您需要将文章替换为文章
public function article(){
$this->belongsTo(Article::class);
}
此外,如果您想获得用户特定的评论,则需要更改您的控制器操作代码
$article = Article::with('user.comment')->find($id) to
$article = Article::with('user.comments')->find($id);
我认为您的问题来自于使用 compact 函数:数组被传递到视图而不是对象。
你能这样试试吗:
// Controller
public function view($id) {
$article = Article::findOrFail($id);
return view('front.single', $article);
}
<!-- View -->
@foreach($article->comments as $comment)
{{ $comment->user->name }}
{{ $comment->comment }}
{{ $comment->created_at->diffForHumans() }}
@endforeach