获取在 Laravel 中评论票证的用户的不同电子邮件
Get distinct emails of users who commented on a ticket in Laravel
我有以下型号。
门票
class Ticket extends Model
{
protected $fillable = ['title', 'content', 'slug', 'status', 'user_id'];
protected $guarded = ['id'];
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
public function comments()
{
return $this->hasMany('App\Comment', 'post_id', 'id');
}
public function commenters()
{
return $this->hasManyThrough('App\User', 'App\Comment');
}
}
评论
class Comment extends Model
{
protected $guarded = ['id'];
public function user()
{
return $this->belongsTo('App\User', 'user_id', 'id');
}
public function ticket()
{
return $this->belongsTo('App\Ticket', 'post_id', 'id');
}
public function post()
{
return $this->morphTo();
}
}
用户
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword;
protected $guarded = ['id'];
protected $hidden = [
'password', 'remember_token',
];
}
- 一张票有很多评论
- 一个评论有一个用户
我正在尝试提取对票证发表评论的用户的姓名字符串列表,但没有成功。
在我的控制器中,我使用以下代码提取评论者列表。
Ticket::where('id', $comment->post_id)->commenters
但是,我遇到了错误:
Property [commenters] does not exist on the Eloquent builder instance.
您缺少闭包。此查询未完成:
Ticket::where('id', $comment->post_id)
未完成的查询是 Builder
class 的一个实例,而不是 Ticket
个实例,或 Ticket
个实例的 Collection
重新期待。
如果您需要一个 Ticket
实例,那么您可以使用 ->first()
:
$ticket = Ticket::where('id', $comment->post_id)->with(['commenters'])->first();
$commenters = $ticket->commenters;
如果您希望有多个 Ticket
个实例,那么您可以使用 ->get()
:
$tickets = Ticket::where('id', $comment->post_id)->with(['commenters'])->get();
foreach($tickets AS $ticket){
$commenters = $ticket->commenters;
}
注:->with(['commenters'])
用于加速->commenters
的加载;如果您省略它,那么当您尝试访问 $ticket->commenters
时,一个新查询是 运行。
我有以下型号。
门票
class Ticket extends Model
{
protected $fillable = ['title', 'content', 'slug', 'status', 'user_id'];
protected $guarded = ['id'];
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
public function comments()
{
return $this->hasMany('App\Comment', 'post_id', 'id');
}
public function commenters()
{
return $this->hasManyThrough('App\User', 'App\Comment');
}
}
评论
class Comment extends Model
{
protected $guarded = ['id'];
public function user()
{
return $this->belongsTo('App\User', 'user_id', 'id');
}
public function ticket()
{
return $this->belongsTo('App\Ticket', 'post_id', 'id');
}
public function post()
{
return $this->morphTo();
}
}
用户
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword;
protected $guarded = ['id'];
protected $hidden = [
'password', 'remember_token',
];
}
- 一张票有很多评论
- 一个评论有一个用户
我正在尝试提取对票证发表评论的用户的姓名字符串列表,但没有成功。
在我的控制器中,我使用以下代码提取评论者列表。
Ticket::where('id', $comment->post_id)->commenters
但是,我遇到了错误:
Property [commenters] does not exist on the Eloquent builder instance.
您缺少闭包。此查询未完成:
Ticket::where('id', $comment->post_id)
未完成的查询是 Builder
class 的一个实例,而不是 Ticket
个实例,或 Ticket
个实例的 Collection
重新期待。
如果您需要一个 Ticket
实例,那么您可以使用 ->first()
:
$ticket = Ticket::where('id', $comment->post_id)->with(['commenters'])->first();
$commenters = $ticket->commenters;
如果您希望有多个 Ticket
个实例,那么您可以使用 ->get()
:
$tickets = Ticket::where('id', $comment->post_id)->with(['commenters'])->get();
foreach($tickets AS $ticket){
$commenters = $ticket->commenters;
}
注:->with(['commenters'])
用于加速->commenters
的加载;如果您省略它,那么当您尝试访问 $ticket->commenters
时,一个新查询是 运行。