Php 如何获取评论的名称';作者
以下是我的表格结构:Php 如何获取评论的名称';作者,php,laravel,polymorphic-associations,Php,Laravel,Polymorphic Associations,以下是我的表格结构: -- users +----+-------+ | id | name | +----+-------+ | 1 | John | | 2 | Jack | | 3 | Peter | +----+-------+ -- posts +----+---------+----------------------+-----------+ | id | title | body | author_id | +----+-----
-- users
+----+-------+
| id | name |
+----+-------+
| 1 | John |
| 2 | Jack |
| 3 | Peter |
+----+-------+
-- posts
+----+---------+----------------------+-----------+
| id | title | body | author_id |
+----+---------+----------------------+-----------|
| 1 | title1 | somthing | 2 |
| 2 | title2 | whatever | 1 |
| 3 | title3 | anything | 3 |
+----+---------+----------------------+-----------+
-- comments
+----+-----------------+---------+-----------+
| id | message | post_id | author_id |
+----+-----------------+---------+-----------+
| 1 | my message | 3 | 2 |
| 2 | whatever | 1 | 3 |
+----+-----------------+---------+-----------+
现在我想得到一个帖子及其所有评论。这是我的密码:
$post= Posts::orderBy('id', 'DESC')->where('id', $request->id)->first();
$comments = $post->comments;
注意,我在用户
模型中有这种关系:
public function comments()
{
return $this->hasMany('App\Comments','post_id', 'id')->orderBy('id');
}
我的问题是什么?我还想知道评论作者的姓名。我指的是写评论的人的名字。无论如何,我如何在现有关系上建立关系
注意:我可以通过原始
加入来实现。但我想知道如何通过拉威尔关系实现这一点?您可以像这样连接两个表
DB::table('comments')
->join('users', function ($join) {
$join->on('comments.author_id', '=', 'users.id');
})
->get();
您可以像这样连接这两个表
DB::table('comments')
->join('users', function ($join) {
$join->on('comments.author_id', '=', 'users.id');
})
->get();
为什么要定义与任务id的关系
在用户模型中:
public function comments()
{
return $this->hasMany('App\Comments','author_id', 'id');
}
在注释模型中:
/**
* comments belongs to a user.
*/
public function user()
{
return $this->belongsTo('App\User', 'author_id', 'id');
}
现在你可以得到一个有评论的用户
User::where("id",$userId)->with("comments")->orderBy('id', 'DESC')->get();
若你们想得到所有评论的帖子,你们应该为帖子模型定义这样的关系
在帖子中
模型定义一个关系:
public function comments()
{
return $this->hasMany('App\Comments','post_id', 'id');
}
在模型中:
/**
* comments belongs to a post.
*/
public function post()
{
return $this->belongsTo('App\Posts', 'post_id', 'id');
}
现在:
为什么定义与任务\u id
的关系
在用户模型中:
public function comments()
{
return $this->hasMany('App\Comments','author_id', 'id');
}
在注释模型中:
/**
* comments belongs to a user.
*/
public function user()
{
return $this->belongsTo('App\User', 'author_id', 'id');
}
现在你可以得到一个有评论的用户
User::where("id",$userId)->with("comments")->orderBy('id', 'DESC')->get();
若你们想得到所有评论的帖子,你们应该为帖子模型定义这样的关系
在帖子中
模型定义一个关系:
public function comments()
{
return $this->hasMany('App\Comments','post_id', 'id');
}
在模型中:
/**
* comments belongs to a post.
*/
public function post()
{
return $this->belongsTo('App\Posts', 'post_id', 'id');
}
现在:
很抱歉,任务id是错的,艾德。只有一个问题,除了()的,
?我已经测试了你的解决方案。但是没有一个不是我问题的答案。正如我在问题中提到的,我所要做的就是获取评论作者的姓名$选择发布->评论()->获取();这是你的答案:)你只需要这样做:如果你想知道注释id 1是哪个用户的?Comment::where(“id”,$commentId)->with(“user”)->first();很抱歉,任务id是错的,艾德。只有一个问题,除了()的,
?我已经测试了你的解决方案。但是没有一个不是我问题的答案。正如我在问题中提到的,我所要做的就是获取评论作者的姓名$选择发布->评论()->获取();这是你的答案:)你只需要这样做:如果你想知道注释id 1是哪个用户的?Comment::where(“id”,$commentId)->with(“user”)->first();