laravel morph(다형성) whereHas

Using WhereHas in Laravel Polymorphic Relations

 

다형성 (morph) 에서 지원하는 아이템 종류만큼 관계를 선언해 문제를 해결합니다. 

현재 제가 사용하고 있는 라라벨 버전은 5.5입니다.

상위 버전에서 위와 같은 방법이 아닌 라라벨에서 제공하는 기능을 통해 해결할 수 있는 걸로 압니다.

 

public function bills()
{
    return $this->belongsTo(Bill::class, 'sponsorable_id')
        ->whereSponsorableType(Bill::class);
}

public function lists()
{
    return $this->belongsTo(List::class, 'sponsorable_id')
        ->whereSponsorableType(List::class);
}

  • share