Laravel whereHas to realted model, whereHas to that related modal not working
Laravel whereHas to realted model, and whereHas to that related modal not working
我有三个 table:
交易模式:
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' 模型 wherehas 'room' 模型
但是下面的查询显示错误,这个查询格式是否正确?
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();
我有三个 table:
交易模式:
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' 模型 wherehas 'room' 模型 但是下面的查询显示错误,这个查询格式是否正确?
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();