首页 > 解决方案 > 从 laravel 的数据库中获取所有带有评论的帖子

问题描述

我有一个系统,其中有人users创建帖子,他们也可以发表评论posts

以下是迁移:

users桌子

public function up()
{
    Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('gender')->default('Male');
    $table->string('email')->unique();
    $table->string('city')->default('Peshawar');
    $table->string('avatar')->default('user.png');
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});
}

posts桌子

    public function up()
{
    Schema::create('posts', function (Blueprint $table) {


        $table->bigIncrements('p_id');
        $table->text('description');
        $table->integer('user_id');   // this should be user id nothing else
        $table->timestamps();

    });
}

comments桌子

    public function up()
{
    Schema::create('comments', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('user_id'); // who has done comment
        $table->integer('post_id');  // on which post comment has been done
        $table->text('body');
        $table->timestamps();
    });
}

楷模

User模型

class User extends Authenticatable{
use Notifiable;

// custom function used for relationshisp
// user can have many posts

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

// user can have many comments as well

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

}

Post模型

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


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

 }

Comment模型

class Comment extends Model

{

// doing this we could insert data into comment table
protected $fillable =['user_id','post_id','body'];
// we need two(2) relations here
// relation of comment with post

// relation of comment with user

// 1. 评论属于一篇文章

public function post(){
    return $this->belongsTo('App\Post');
}

// 2. 评论也属于用户

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

}

route

Route::get('/home', 'HomeController@index')->name('home');

这是中的index方法HomeController

public function index(){
    $posts = Post::with('comments')->get();
    // $comments = Comment::all();

    return view('home')->with( array('posts'=>$posts ) );
}

问题

因此,当我访问时,/home我希望获得所有带有他们评论的帖子。

正如你所看到的,我已经posts用他们comment这样的方式检索了:

$posts = Post::with('comments')->get();

然后我将它传递给home.blade.php. 这是我为home.blade.php完成这项任务所做的工作。

看法 /home.blade.php

@foreach($posts as $post)

     <h4 class="media-heading">{{$post->user->name}}</h4>
     <small>{{$post->created_at}}</small>

     <p>{{$post->description}}</p>

     <!-- trying to retrieve comments with each post -->

             @if (count($post->comments))
                  @foreach($post->commnents as $comment)
                      <small>$comment->body</small>
                   @endforeach
             @else 
                  No comments Found
             @endif 




 @endforeach

它给了我这个错误

未定义变量:post(查看:F:\application\resources\views\home.blade.php)

牢记这些models以及他们relationships彼此之间的关系,我是否以错误的方式来检索每个帖子的评论?如果是这样,我怎样才能得到posts他们的所有comments东西,如果没有comments,应该说no comments found

这是dd($posts) dd($posts) 返回 this的结果,看看评论字段,它是空的。

network选项卡显示以下这个 请帮助,谢谢大家。

标签: phplaravel

解决方案


大概是因为这个。。

public function up()
{
    Schema::create('posts', function (Blueprint $table) {


        $table->bigIncrements('p_id'); <---
        $table->text('description');
        $table->integer('user_id');   // this should be user id nothing else
        $table->timestamps();

    });
}

您使用 p_id 而不是 id 的任何原因?关系工作,以便帖子的 id 与评论表中的 post_id 匹配。如果您使用它,则必须在创建关系时指定此自定义键。

查看https://laravel.com/docs/5.8/eloquent-relationships#defining-relationships


推荐阅读