【发布时间】:2017-10-24 16:27:26
【问题描述】:
我在 laravel eloquent 中查询多个关系时有点卡在这里我有像
这样的原始查询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
现在我需要将这个原始查询查询到 laravel eloquent 中。我试过这样做
$getSchedule= Schedule::with(['userSchedule' => function($query) use ($userId,$homeCourtId,$timeFrom, $timeTo) {
$query->where(['userId'=> $userId,'homeCourtId'=>$homeCourtId,'userHomeCourtStatus' => Constant::STATUS_1]);
}]) ->where('timeFrom','<=',$timeFrom)
->where('timeTo','>=',$timeTo)
->where(['scheduleStatus'=>Constant::STATUS_0])
->get();
但我没有得到结果,而是得到了我应该得到记录的空白消息数组。
问题: 我做错了什么? 建议我查询此查询的正确方法。
调度模型
public function friendsFeed(){
return $this->hasOne(UserHomeCourt::class,'userHomeCourtId','userHomeCourtId')->with('user')->with('homeCourt');
}
public function userSchedule(){
return $this->hasOne(UserHomeCourt::class,'userHomeCourtId','userHomeCourtId')->with('homeCourt');
}
UserHomeCourt 模型
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');
}
【问题讨论】:
-
where('timeFrom','>=',$timeFrom) ->where('timeTo','<=',$timeTo)条件不同然后查询。 -
那么什么是实现查询结果的正确方法请正确
-
即使在做同样的事情后我也得到空白响应
标签: php mysql laravel eloquent