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

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

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(&#39;home&#39;, [
            &#39;posts&#39; =&gt; $posts,
            &#39;comments&#39; =&gt;  $comment
        ]);
}

My Post.php :

 public function comments() {
        return $this-&gt;hasMany(Comment::class);
    }

  public function user() {
        return $this-&gt;belongsTo(User::class);
    }

My Comments.php :

 public function post() {
        return $this-&gt;belongsTo(Post::class);
    }

 public function user() {
        return $this-&gt;belongsTo(User::class);
    }

My User.php :

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

My home.blade.php :

@foreach ($posts as $post)
        &lt;div class=&quot;box-post&quot;&gt;
          &lt;p&gt;{{ $post-&gt;user-&gt;name }}&lt;/p&gt;
          &lt;p&gt;{{ $post-&gt;post_content}}&lt;/p&gt;
          &lt;a href=&quot;/comment/{{ $post-&gt;id }}&quot;&gt;&lt;button&gt;Comment&lt;/button&gt;&lt;/a&gt;
        &lt;/div&gt;

        @foreach ($comments as $comment)
           &lt;div class=&quot;box-post&quot;&gt;{{ $comment-&gt;comment_content }} &lt;/div&gt;
        @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(&#39;comments&#39;)-&gt;get();
        
        
        
        return view(&#39;home&#39;, [
            &#39;posts&#39; =&gt; $posts,
            
        ]);
}

then in blade

    @foreach ($posts as $post)
        &lt;div class=&quot;box-post&quot;&gt;
          &lt;p&gt;{{ $post-&gt;user-&gt;name }}&lt;/p&gt;
          &lt;p&gt;{{ $post-&gt;post_content}}&lt;/p&gt;
          &lt;a href=&quot;/comment/{{ $post-&gt;id }}&quot;&gt;&lt;button&gt;Comment&lt;/button&gt;&lt;/a&gt;
        &lt;/div&gt;

        @foreach ($post-&gt;comments as $comment)
           &lt;div class=&quot;box-post&quot;&gt;{{ $comment-&gt;comment_content }} &lt;/div&gt;
        @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-&gt;comments can get all comments related on each post.

@foreach ($posts as $post)
        &lt;div class=&quot;box-post&quot;&gt;
          &lt;p&gt;{{ $post-&gt;user-&gt;name }}&lt;/p&gt;
          &lt;p&gt;{{ $post-&gt;post_content}}&lt;/p&gt;
          &lt;a href=&quot;/comment/{{ $post-&gt;id }}&quot;&gt;&lt;button&gt;Comment&lt;/button&gt;&lt;/a&gt;
        &lt;/div&gt;

        @foreach ($post-&gt;comments as $comment) // make changes here
           &lt;div class=&quot;box-post&quot;&gt;{{ $comment-&gt;comment_content }} &lt;/div&gt;
        @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(&#39;comments&#39;, &#39;user&#39;)-&gt;get();
        // eager loading user is needed, cause in blade you call $post-&gt;user-&gt;name
        // calling comments will no longer needed
  
        // return only $posts
        return view(&#39;home&#39;, compact(&#39;posts&#39;));
}

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

@foreach ($posts as $post)
        &lt;div class=&quot;box-post&quot;&gt;
          &lt;p&gt;{{ $post-&gt;user-&gt;name }}&lt;/p&gt;
          &lt;p&gt;{{ $post-&gt;post_content}}&lt;/p&gt;
          &lt;a href=&quot;/comment/{{ $post-&gt;id }}&quot;&gt;&lt;button&gt;Comment&lt;/button&gt;&lt;/a&gt;
        &lt;/div&gt;

        @foreach ($post-&gt;comments as $comment)
           &lt;div class=&quot;box-post&quot;&gt;{{ $comment-&gt;comment_content }} &lt;/div&gt;
        @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.

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:

确定