当前这是我的Laravel查询
$response['appointMent'] = $this->appointment->where('user_id', Auth::guard('user')->user()->id)
->whereIn('status', array('Sched', 'Going', 'Request Appointment'))
->whereDate('set_date', '>', Carbon::now()->subDays(1))
->with(['company'])
->with(['applicant'])
->with(['job'])
->with(['user'])->get();
但是不幸的是,我还想获取公司与用户表之间的连接数据,其中user_id用于公司,而id用于用户
如何在公司阵列下连接用户?
答案 0 :(得分:1)
您需要像这样与用户建立公司关系
公司型号
public function user()
{
return $this->belongsTo(Company::class,'user_id');
}
您的查询将变为
response['appointMent'] = $this->appointment->where('user_id', Auth::guard('user')->user()->id)
->whereIn('status', array('Sched', 'Going', 'Request Appointment'))
->whereDate('set_date', '>', Carbon::now()->subDays(1))
->with(['company.user','applicant','job'])->get();
现在用户关系将在公司内部
或者相反 用户模型
public function company()
{
return $this->hasOne(User::class); //Or hasMany depends on your needs
}
然后将以下查询更改为
response['appointMent'] = $this->appointment->where('user_id', Auth::guard('user')->user()->id)
->whereIn('status', array('Sched', 'Going', 'Request Appointment'))
->whereDate('set_date', '>', Carbon::now()->subDays(1))
->with(['user.company','applicant','job'])->get();