Php 从laravel中的数据库获取所有帖子及其评论

Php 从laravel中的数据库获取所有帖子及其评论,php,laravel,Php,Laravel,我有一个系统,其中有用户创建帖子,他们也可以对帖子发表评论 以下是迁移: 用户表格 public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('gender')->default('Male'); $table

我有一个系统,其中有
用户创建帖子,他们也可以对
帖子发表评论

以下是迁移:

用户
表格

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

    });
}
    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();
    });
}
注释
表格

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

    });
}
    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();
    });
}
型号

用户
型号

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

 }
class Comment extends Model
}

Post
Model

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

 }
class Comment extends Model
注释
型号

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

 }
class Comment extends Model
{

//1.评论属于一个帖子

public function post(){
    return $this->belongsTo('App\Post');
}
//2.评论也属于用户

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

路线

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

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

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

因此,当我访问
/home
时,我想获得所有带有评论的帖子

如您所见,我检索了
帖子
,其中包含
评论
,如下所示:

$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
@foreach($posts as$post)
{{$post->user->name}
{{$post->created_at}
{{$post->description}

@如果(计数($post->comments)) @foreach($post->comments as$comment) $comment->body @endforeach @否则 没有找到评论 @恩迪夫 @endforeach
它给了我这个错误

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

记住那些
模型
以及它们之间的
关系
,我是否以错误的方式检索每个帖子的评论?如果是这样的话,我如何才能获得所有带有
评论的
帖子
,当没有
评论时,应该说
没有找到评论

以下是
dd($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();

    });
}
public function up()
{
Schema::create('posts',函数(Blueprint$表){
$table->big增量('p_id');文本('description');
$table->integer('user_id');//这应该是用户id,而不是其他
$table->timestamps();
});
}
你为什么用p_id代替id?关系起作用,因此post的id与comments表中的post_id匹配。。如果使用此键,则在创建关系时必须指定此自定义键


签出

尝试将post自定义键作为第三个参数传递给关系:

评论模型

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

也许更好的选择是,将表POST的键更改为“id”,以符合Laravel的约定。创建新迁移并运行它

Schema::table('posts', function (Blueprint $table) {
    $table->renameColumn('p_id', 'id');
});

另一个选项是,创建外键约束,以强制数据库级别的引用完整性:

帖子表格

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->bigIncrements('p_id');
        $table->text('description');
        $table->integer('user_id')->unsigned();
        $table->timestamps();

        $table->foreign('user_id')->references('id')->on('users');
    });
}
评论表格

public function up()
{
    Schema::create('comments', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('user_id')->unsigned(); 
        $table->integer('post_id')->unsigned();  
        $table->text('body');
        $table->timestamps();

        $table->foreign('user_id')->references('id')->on('users');
        $table->foreign('post_id')->references('p_id')->on('posts');
    });
}

您必须回滚并重新运行迁移(您将丢失保存在DB中的数据)。

查看错误跟踪,其中哪一行是例外。此外,您在这里有一个输入错误:
@foreach($post->comments
它突出了
if(count($post->comments))
尝试
@php dd($posts)中的问题;@endphp
在home.blade.php的顶部,然后你可以看到帖子中的内容。试着按照porloscerros所说的修复打字错误。请注意,这两个第一选项是MekjkrhG的答案建议的。如果答案有助于解决你的问题,请随意回答;)但这会导致另一个问题,现在ajax评论不起作用了@MekjkrhGHere是代码链接,到目前为止我都有相同的代码,但是在修复之前,动态评论不起作用,您建议它工作良好;啊提醒。。如果将迁移从p_id更改为id。。您必须将使用p_id的代码更新为id。。否则就不行了。你有没有收到任何错误?即使注释没有存储到数据库中,在这次更改之后,我提供了链接@MekjkrhGdid u回滚迁移并将p_id更改为id的代码在那里?在那之后,你还会迁移吗?