gpt4 book ai didi

php - Laravel whereHas to realted model,whereHas to that related modal not working

转载 作者:行者123 更新时间:2023-11-29 05:05:38 25 4
gpt4 key购买 nike

我有三个表:

交易模式:

class Deal extends Model
{
protected $guarded = ['id'];

public function hotel() {
return $this->belongsTo('App\Hotel');
}
}

酒店模型:

class Hotel extends Model
{
public function room(){
return $this->hasMany('App\Room');
}

public function deal(){
return $this->hasMany('App\Deal');
}
}

房间模型:

class Room extends Model
{

public function hotel(){
return $this->belongsTo('App\Hotel');
}

}

下面的查询工作正常,

return $greatDeals = Deal::whereHas('hotel', function ($query) {
$query->Where('astatus', 1)->Where('status', 0);
})->get();

但是我想查询'hotel' model wherehas 'room' model但是下面的查询显示错误,这个查询格式是否正确?

 return $greatDeals = Deal::whereHas('hotel', function ($query) {
$query->whereHas('room', function ($query) {
$query->Where('astatus', 1)->Where('status', 0);
})->get();
})->get();

错误:

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'deals.hotel_id' in 'where clause' (SQL: select * from `hotels` where `deals`.`hotel_id` = `hotels`.`id` and exists (select * from `rooms` where `hotels`.`id` = `rooms`.`hotel_id` and `astatus` = 1 and `status` = 0)) ◀"

最佳答案

删除第一个get():

Deal::whereHas('hotel', function ($query) {
$query->whereHas('room', function ($query) {
$query->where('astatus', 1)->where('status', 0);
});
})->get();

或者这样做:

Deal::whereHas('hotel.room', function ($query) {
$query->where('astatus', 1)->where('status', 0);
})->get();

关于php - Laravel whereHas to realted model,whereHas to that related modal not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48802245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com