Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在laravel eloquent中查询多个关系_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

Php 如何在laravel eloquent中查询多个关系

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

当我在laravel Elotent中查询多个关系时,我有点被困在这里,我的原始查询如下

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')