我有三张桌子:
交易模型:
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();但我想查询的“酒店”模型中有“房间”模型,但下面的查询显示错误,这种查询格式正确吗?
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)) ◀"发布于 2018-02-15 07:49:33
删除第一个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();https://stackoverflow.com/questions/48802245
复制相似问题