【发布时间】:2020-12-06 12:58:35
【问题描述】:
我有用户解释问题的模型票。
Ticket.php
class Ticket extends Model
{
protected $table = 'tickets';
/**
* @var array
*/
protected $guarded = [];
/**
* @var array
*/
protected $hidden = [
'created_at', 'updated_at'
];
public function ticket_replies()
{
return $this->hasMany(TicketReply::class, 'ticket_id');
}
public function ticket_assigned_agents()
{
return $this->hasMany(TicketAssignedAgent::class, 'ticket_id');
}
}
还有一个型号 TicketReply.php
class TicketReply extends Model
{
protected $table = 'ticket_replies';
/**
* @var array
*/
protected $guarded = [];
/**
* @var array
*/
protected $hidden = [
'created_at', 'updated_at'
];
public function staffs(){
return $this->belongsTo(Staff::class,'user_id');
}
public function ticket()
{
return $this->belongsTo(Ticket::class, 'ticket_id');
}
}
现在我想从工单回复中获取员工姓名
查询
public function getReplies($ticket_id)
{
$ticket = Ticket::where('id',$ticket_id)->with('ticket_replies')->first();
return response()->json($ticket);
}
我想在 ajax 成功中从 TicketReply 模型中获取员工姓名。
$.each(ticket.ticket_replies, function(index, reply) {
console.log(reply.staffs.name);
}
但它不起作用。我该怎么办?
【问题讨论】:
标签: ajax laravel laravel-5 eloquent