Php Laravel何处必须是真实模型,何处必须是相关模型不起作用
我有三张桌子: 交易模式:Php Laravel何处必须是真实模型,何处必须是相关模型不起作用,php,mysql,laravel,laravel-5.5,Php,Mysql,Laravel,Laravel 5.5,我有三张桌子: 交易模式: class Deal extends Model { protected $guarded = ['id']; public function hotel() { return $this->belongsTo('App\Hotel'); } } class Hotel extends Model { public function room(){ return $this->hasMan
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');
}
}
酒店模式:
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');
}
}
房间型号:
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)) ◀"
删除第一个get()
:
或者这样做:
Deal::whereHas('hotel.room', function ($query) {
$query->where('astatus', 1)->where('status', 0);
})->get();
两者都很好,我选择第一个,因为我需要为酒店添加相同的where条件($query->where('astatus',1)->where('status',0);)。@Alexey Mezenin。你无处不在,:)