【问题标题】:How to query multiple relationships in laravel eloquent如何在 laravel eloquent 中查询多个关系
【发布时间】: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');
}

我从 SQL Query得到的回复

【问题讨论】:

  • where('timeFrom','&gt;=',$timeFrom) -&gt;where('timeTo','&lt;=',$timeTo) 条件不同然后查询。
  • 那么什么是实现查询结果的正确方法请正确
  • 即使在做同样的事情后我也得到空白响应

标签: php mysql laravel eloquent


【解决方案1】:

从你的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();

【讨论】:

  • 同样的空白消息数组 :(
  • @BhavikBamania 我刚刚注意到您的主查询中有 3 个表。你能分享你的关系吗
  • 是的,在用户计划模型中我已经关联了这两个表,这就是我在这里使用它的原因
  • 是的,给我一秒钟
  • @BhavikBamania 在您的帖子中分享 3 个模型数据。我会制作正确的查询。
【解决方案2】:

尝试使用whereHas函数:

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

【讨论】:

  • 还是一样的东西..消息数组中没有任何内容
  • 为什么你在关系中使用相同的foreign_idlocal_id,应该是不同的
  • 你能添加你的表结构吗
  • 所以tblSchedules中的主键是userHomeCourtId?
  • 所以你在tblSchedules中有scheduleId并且它与tblUserHomeCourts中的scheduleId相关,那么关系应该是hasOne(UserHomeCourt::class,'scheduleId','scheduleId')而不是
猜你喜欢
  • 2014-10-31
  • 1970-01-01
  • 2013-10-03
  • 2017-07-19
  • 2017-11-03
  • 2019-02-15
  • 2021-09-25
  • 2015-01-21
  • 2017-01-14
相关资源
最近更新 更多