php - 从 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的结果,看看评论字段,它是空的。
解决方案
大概是因为这个。。
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
推荐阅读
- javascript - React JS - 状态不返回 false
- vue.js - 是否可以在 watch() 函数 vue 中访问变量
- javascript - 带控件的图像幻灯片
- python - Pandas 只能将大小为 1 的数组转换为 Python 标量
- javascript - Node/NPM:如果我升级我的节点版本,我应该重建包锁定文件吗?
- angular - 如何从 Name 属性中获取 ID 属性。角度可观察 rxjs
- javascript - wkhtmltopdf 和 pdfkit 从 url 返回一个空白 pdf
- javascript - Mongoose - findByIdAndUpdate 和 req.body 删除键值
- python - 性能仪表中的 Web 抓取数据
- elasticsearch - Elasticsearch 无法运行