1

I am trying this Query in Laravel 5.2

User::with(['posts'])
  ->withCount('post_images')
  ->orderBy('post_images_count', 'desc')
  ->take(8)
  ->get();

After that I got this error Call to undefined method Illuminate\Database\Query\Builder::post_images()

I did not understand what mistake is here.

Here users table has relation with posts Table and Posts table has relation with post_images table

public function posts()
{
    return $this->hasMany(Post::class);
}

public function postimages()
{
    return $this->hasManyThrough(PostImage::class, Post::class);
}

Please guide How can I fix this.

Harry
  • 139
  • 1
  • 7

1 Answers1

1

As @linktoahref said, withCount is taking a relation method names as argument, not a table nor a column name.

withCount('postimages')should fix your problem