问题描述
我正在尝试使用关系的“with”和“whereHas”过滤表,并使其遵循第二个关系。
是否可以使用“with”来实现,或者只能使用“Joins”来实现?
Ticket >> StatusHistory(最后一条记录)>> StatusName = 'new'
ticket
-id
-name
status_history
- ticket_id
- status_name_id
- timestamps
status_names
- id
- name (new,close,paused)
<?
class Ticket extends Model
{
public function latestStatus()
{
return $this->hasOne(StatusHistory::class,'ticket_id','id')->latest();
}
class StatusHistory extends Model
{
public function statusName()
{
return $this->hasOne(StatusName::class,'id','status_name_id');
}
This usually works well if there is only one Status history record,but if there are more,it returns values that should not be there.
example: ticket_id 1 has in history first status new and them status paused
With this sentence he returned the ticket to me even so he no longer has the last status in "new".
Ticket::with('latestStatus')
->whereHas('latestStatus.statusName',function($q){
$q->where('name','new');
})
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)