Thursday, February 15, 2018

Laravel whereHas to realted model, and whereHas to that related modal not working - development

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

Im having three table:

Deal Model:

class Deal extends Model
{
    protected $guarded = ['id'];

    public function hotel() {
        return $this->belongsTo('AppHotel');
    }
}

Hotel Model:

class Hotel extends Model
{
    public function room(){
        return $this->hasMany('AppRoom');
    }

    public function deal(){
        return $this->hasMany('AppDeal');
    }
}

Room Model:

class Room extends Model
{

    public function hotel(){
        return $this->belongsTo('AppHotel');
    }

}

The below query works fine,

return $greatDeals = Deal::whereHas('hotel', function ($query) {
                $query->Where('astatus', 1)->Where('status', 0);
            })->get();

but i want to query the ‘hotel’ model wherehas ‘room’ model
but the below query shows error, was this query format is correct?

 return $greatDeals = Deal::whereHas('hotel', function ($query) {
                    $query->whereHas('room', function ($query) {
                        $query->Where('astatus', 1)->Where('status', 0);
                    })->get();
                })->get();

The error:

"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)) ◀"



from Laravel Questions and Answers https://laravelquestions.com/php/laravel-wherehas-to-realted-model-and-wherehas-to-that-related-modal-not-working/
via Lzo Media

No comments:

Post a Comment