亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 209 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號