簡體   English   中英

Laravel哪里有實體模型,哪里有相關的模態不起作用

[英]Laravel whereHas to realted model, and whereHas to that related modal not working

我有三張桌子:

交易模式:

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', 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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM