【问题标题】:Get the user name and comment with article id in Laravel在 Laravel 中获取用户名和带有文章 id 的评论
【发布时间】:2018-03-28 17:27:40
【问题描述】:

我有 3 个表用户、cmets 和文章。我有这样的路线:

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);
}

这是我的表结构: -用户(id、姓名、电子邮件、密码、remember_token、created_at、updated_at) -cmets(id、user_id、article_id、comment、created_at、updated_at) -文章(id、user_id、标题、内容、created_at、updated_at)

那么我怎样才能通过使用这条路线来获得用户名?谢谢。

【问题讨论】:

    标签: laravel laravel-5 eloquent eager-loading


    【解决方案1】:

    在您的评论模型上,您需要将文章替换为文章

    public function article(){
            $this->belongsTo(Article::class);
        }
    

    另外,如果您想获取用户特定的 cmets,则需要从

    更改您的控制器操作代码
    $article = Article::with('user.comment')->find($id) to
    $article = Article::with('user.comments')->find($id); 
    

    【讨论】:

    • 现在我收到此错误“尝试获取非对象的属性(查看:E:\xampp\htdocs\blog\resources\views\front\commentList.blade.php)(查看:E :\xampp\htdocs\blog\resources\views\front\ ▶"
    • 如果你想列出文章的所有cmets,那么你只需要在你的文章中附加cmets。 $article = Article::with('cmets')->find($id);
    • 在您的评论模型上,您需要将用户更改为用户 public function user(){ $this->belongsTo(User::class); }
    • 当我做所有事情时,我得到了这个错误 App\Comment::user must return a relationship instance.
    • 能否请您用我上面提到的所有更改更新您的问题,以便我更好地了解
    【解决方案2】:

    我认为您的问题来自 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
    

    【讨论】:

    • 它给了我这种错误“未定义的变量:文章(查看:E:\xampp\htdocs\blog\resources\views\front\single.blade.php)”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    相关资源
    最近更新 更多