Laravel 调用未定义的方法 Illuminate\Database\Query\Builder::post_images()
Laravel Call to undefined method Illuminate\Database\Query\Builder::post_images()
我正在 Laravel 5.2
中尝试此查询
User::with(['posts'])
->withCount('post_images')
->orderBy('post_images_count', 'desc')
->take(8)
->get();
之后我得到这个错误Call to undefined method Illuminate\Database\Query\Builder::post_images()
我不明白这是什么错误。
此处 users table
与 posts Table
相关,帖子 table 与 post_images table
相关
public function posts()
{
return $this->hasMany(Post::class);
}
public function postimages()
{
return $this->hasManyThrough(PostImage::class, Post::class);
}
请指导如何解决这个问题。
正如@linktoahref 所说,withCount
将关系方法名称作为参数,而不是 table 或列名。
withCount('postimages')
应该可以解决您的问题
我正在 Laravel 5.2
中尝试此查询User::with(['posts'])
->withCount('post_images')
->orderBy('post_images_count', 'desc')
->take(8)
->get();
之后我得到这个错误Call to undefined method Illuminate\Database\Query\Builder::post_images()
我不明白这是什么错误。
此处 users table
与 posts Table
相关,帖子 table 与 post_images table
public function posts()
{
return $this->hasMany(Post::class);
}
public function postimages()
{
return $this->hasManyThrough(PostImage::class, Post::class);
}
请指导如何解决这个问题。
正如@linktoahref 所说,withCount
将关系方法名称作为参数,而不是 table 或列名。
withCount('postimages')
应该可以解决您的问题