binggle
2 months ago

addEagerConstraints error with relationship.

Posted 2 months ago by binggle

I have two models. Webtoon and Episode .

1 Webtoon has many Episodes.

in model Webtoon


use App\Models\Episode;
use Illuminate\Database\Eloquent\Model;

class Webtoon extends Model
{

  function episodes(){
    return $this->hasMany( Episode::class );
  }

  public function lastEpisode()
  {
      return $this->hasMany(Episode::class)->orderBy('published_at', 'desc')->first();
  }

}

in Episode Model


namespace App\Models;

use App\Models\Webtoon;
use Illuminate\Database\Eloquent\Model;

class Episode extends Model
{

  function webtoon(){
    return $this->belongsTo( Webtoon::class);
  }
}

in Controller


$lists = Webtoon::with('lastEpisode')
                ->orderBy('created_at', 'desc')
                ->paginate( $perPage);
dd($lists->toArray());

it goes error following


BadMethodCallException
Call to undefined method App\Models\Episode::addEagerConstraints()

What is my mistake ?

Please sign in or create an account to participate in this conversation.