Php 如何在laravel eloquent中查询多个关系
当我在laravel Elotent中查询多个关系时,我有点被困在这里,我的原始查询如下Php 如何在laravel eloquent中查询多个关系,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,当我在laravel Elotent中查询多个关系时,我有点被困在这里,我的原始查询如下 SELECT * FROM tblSchedules,tblUserHomeCourts,tblHomeCourts where tblHomeCourts.userId=6 and tblHomeCourts.homeCourtId=5 and (tblSchedules.timeFrom <= 1495617580 and tblSchedules.timeTo >= 1
SELECT * FROM tblSchedules,tblUserHomeCourts,tblHomeCourts
where tblHomeCourts.userId=6
and tblHomeCourts.homeCourtId=5
and (tblSchedules.timeFrom <= 1495617580
and tblSchedules.timeTo >= 1495617580)
and tblUserHomeCourts.userHomeCourtStatus=1
and tblSchedules.scheduleStatus=0
用户家庭法庭模型
public function homeCourt(){
return $this->belongsTo(HomeCourt::class,'homeCourtId','homeCourtId');
}
public function user(){
return $this->belongsTo(User::class,'userId','userId');
}
public function friendsFeed()
{
return $this->hasOne(UserHomeCourt::class, 'homeCourtId', 'homeCourtId');
}
public function userSchedule()
{
return $this->hasOne(UserHomeCourt::class, 'homeCourtId', 'homeCourtId');
}
}
主场模式
public function homeCourt(){
return $this->belongsTo(HomeCourt::class,'homeCourtId','homeCourtId');
}
public function user(){
return $this->belongsTo(User::class,'userId','userId');
}
public function friendsFeed()
{
return $this->hasOne(UserHomeCourt::class, 'homeCourtId', 'homeCourtId');
}
public function userSchedule()
{
return $this->hasOne(UserHomeCourt::class, 'homeCourtId', 'homeCourtId');
}
我从SQL查询得到的响应从SQL到eloquent的时间检查是错误的,当您将多个条件传递到
其中
,您需要将条件设置为数组
$getSchedule= Schedule::with(['userSchedule' => function($query) use ($userId, $homeCourtId) {
$query->whereHas('homeCourt', function ($query) use ($userId, $homeCourtId) {
$query->where('userId', $userId)
->where('homeCourtId', $homeCourtId);
})->where('userHomeCourtStatus', 1);
}])
->where('timeFrom', '<=', $timeFrom)
->where('timeTo', '>=', $timeTo)
->where('scheduleStatus', 0)
->get();
$getSchedule=Schedule::with(['userSchedule'=>函数($query)use($userId,$homeCourtId){
$query->whereHas('homeCourt',函数($query)use($userId,$homeCourtId){
$query->where('userId',$userId)
->其中('homeCourtId',$homeCourtId);
})->其中('userHomeCourtStatus',1);
}])
->其中('timeFrom','=',$timeTo)
->其中('scheduleStatus',0)
->get();
从sql到eloquent的时间检查是错误的,当您将多个条件传递到where
时,需要将条件设置为数组
$getSchedule= Schedule::with(['userSchedule' => function($query) use ($userId, $homeCourtId) {
$query->whereHas('homeCourt', function ($query) use ($userId, $homeCourtId) {
$query->where('userId', $userId)
->where('homeCourtId', $homeCourtId);
})->where('userHomeCourtStatus', 1);
}])
->where('timeFrom', '<=', $timeFrom)
->where('timeTo', '>=', $timeTo)
->where('scheduleStatus', 0)
->get();
$getSchedule=Schedule::with(['userSchedule'=>函数($query)use($userId,$homeCourtId){
$query->whereHas('homeCourt',函数($query)use($userId,$homeCourtId){
$query->where('userId',$userId)
->其中('homeCourtId',$homeCourtId);
})->其中('userHomeCourtStatus',1);
}])
->其中('timeFrom','=',$timeTo)
->其中('scheduleStatus',0)
->get();
尝试使用具有功能的:
Schedule::whereHas('userSchedule', function ($query)
{
$query->where('userId', $userId)
->where('homeCourtId', $homeCourtId)
->where('userHomeCourtStatus', Constant::STATUS_1);
})
->where('timeFrom', '<=', $timeFrom)
->where('timeTo', '>=', $timeTo)
->where('scheduleStatus', Constant::STATUS_0)
->get();
Schedule::whereHas('userSchedule',函数($query)
{
$query->where('userId',$userId)
->其中('homeCourtId',$homeCourtId)
->其中('userHomeCourtStatus',常量::STATUS_1);
})
->其中('timeFrom','=',$timeTo)
->其中('scheduleStatus',常量::STATUS_0)
->get();
尝试使用具有
功能的:
Schedule::whereHas('userSchedule', function ($query)
{
$query->where('userId', $userId)
->where('homeCourtId', $homeCourtId)
->where('userHomeCourtStatus', Constant::STATUS_1);
})
->where('timeFrom', '<=', $timeFrom)
->where('timeTo', '>=', $timeTo)
->where('scheduleStatus', Constant::STATUS_0)
->get();
Schedule::whereHas('userSchedule',函数($query)
{
$query->where('userId',$userId)
->其中('homeCourtId',$homeCourtId)
->其中('userHomeCourtStatus',常量::STATUS_1);
})
->其中('timeFrom','=',$timeTo)
->其中('scheduleStatus',常量::STATUS_0)
->get();
<代码>哪里(‘TimeOn’,‘C=’,$TimeOn’)>何处(‘Timto to’,‘哪一个’到‘To’’,然后什么是实现查询结果的正确方式,请纠正它,即使在做同样的事情之后,我也得到空白响应<代码>哪里(‘TimeOn’,‘>’,$TimeOn)->(Timto to),然后什么是正确的方式来实现查询结果,请改正它,即使在做同样的事情之后,我也得到空白应答邮件空白消息数组:(@BhavikBamania我刚注意到你的主查询中有3个表。你能在用户计划模型中共享你的关系吗?我已经将这两个表关联起来了,这就是为什么我在这里使用这两个表是的,给我一个sec@BhavikBamania在您的帖子中共享3个模型数据。我将创建正确的查询。相同的内容空白消息数组:(@BhavikBamania我刚注意到你的主查询中有3个表。你能在用户计划模型中共享你的关系吗?我已经将这两个表关联起来了,这就是为什么我在这里使用这两个表是的,给我一个sec@BhavikBamania在你的帖子中共享3个模型数据。我将创建正确的查询。仍然是一样的。消息数组中没有任何内容。你使用的是什么如果在关系中使用相同的foreign\u id
和local\u id
,则应该有所不同。可以添加表结构,因此tblSchedules
中的主键是userHomeCourtId
?因此在tblSchedules
中有scheduleId
,并且它与tblUserHomeCourt>中的scheduleId
相关s
,则关系应为hasOne(UserHomeCourt::class、'scheduleId'、'scheduleId')
而不是同样的东西。消息数组中没有任何内容为什么在关系中使用相同的外来id
和本地id
,它应该是不同的可以添加表结构所以tblSchedules
中的主键是userHomeCourtId
?所以在tblSchedules中有scheduleId
并且它与tblUserHomeCourts
中的scheduleId
相关,那么关系应该是hasOne(UserHomeCourt::class,'scheduleId','scheduleId')