简体   繁体   English

如何在Laravel中获取$ comments-> post_id = $ post-> id的注释?

[英]How to fetch comments where $comments->post_id = $post->id with Laravel?

Okay, 好的,

So I can't figure this one out. 所以我无法弄清楚这一点。 Eloquent model is new to me. 雄辩的榜样对我来说是新的。

I'm trying to fetch comments for specific posts. 我正在尝试获取特定帖子的评论。

This is my post model : 这是我的帖子模型

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public function user()
    {
        return $this->belongsTo('App\User'); 
    }

    public function likes()
    {
        return $this->hasMany('App\Like');
    }

    public function comments()
    {
        return $this->hasMany('App\Comment');
    }
}

Comment model: 评论模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    public function user()
    {
        return $this->belongsTo('App\User');
    }
    public function post()
    {
        return $this->belongsTo('App\Post');
    }
}

Routes: 路线:

public function getDashboard(){
        $posts = Post::orderBy('created_at', 'desc')->get(); // MySQL ORDER BY
        $comments = Comment::orderBy('created_at', 'desc')->get();

        return view('dashboard', ['posts' => $posts], ['comments' => $comments]);
    }

($comments might be unnecessary?) (可能不需要$ comments?)

View: 视图:

@foreach($posts as $post)

        <article class="post" data-postid=" {{ $post->id }} ">
            <p>{{ $post->body }}</p>
            <div class="info">
                Posted by {{$post->user->first_name}}  on {{ $post->created_at }}
            </div>
</article>

I tried to loop through the comments using: 我试图使用以下方式遍历注释:

@while($comments->post_id = $post->id)

   <p>{{$comments->body}}</p>

@endwhile

I got an error message "Undefined property: Illuminate\\Database\\Eloquent\\Collection::$body" even though the comments table have a column named "body". 我收到一条错误消息“未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ body”,即使注释表中有一个名为“ body”的列。

@foreach($post->comments as $comment)

Is what you want. 是你想要的。 You can use eager loading in the query to speed this up as well: 您也可以在查询中使用紧急加载来加快速度:

Post::with('comments')->orderBy('created_at', 'desc')->get()

You code with create multiple queries because of $post->user->first_name . 由于$post->user->first_name使用创建多个查询进行编码。 For example, if you have 70 posts, your code will create 72 query which is awful. 例如,如果您有70条帖子,那么您的代码将创建72条糟糕的查询。

It's much better to use eager loading to load posts with comments and users. 最好使用渴望加载来加载带有评论和用户的帖子。 This code will create just 3 queries: 此代码将仅创建3个查询:

Post::orderBy('created_at', 'desc')
    ->with(['user', 'comments' => function ($q) {
        $q->orderBy('created_at', 'desc');
    }])->get();

And then itarate over collection: 然后评估收藏:

@foreach ($posts as $post) {
    <article class="post" data-postid=" {{ $post->id }} ">
    ....
    @foreach ($post->comments as $comment) {
        $comment->content;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM