Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel何处必须是真实模型,何处必须是相关模型不起作用_Php_Mysql_Laravel_Laravel 5.5 - Fatal编程技术网

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。你无处不在,:)