为了账号安全,请及时绑定邮箱和手机立即绑定

Laravel 中的查询 - 相当于 left join

Laravel 中的查询 - 相当于 left join

PHP
江户川乱折腾 2023-05-12 10:28:54
目前这是我的 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这是表公司表:和用户表如何连接公司数组下的用户?
查看完整描述

1 回答

?
交互式爱情

TA贡献1712条经验 获得超3个赞

您需要像这样为公司与用户建立关系


公司模式


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();


查看完整回答
反对 回复 2023-05-12
  • 1 回答
  • 0 关注
  • 154 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信