BadMethodCallException 调用未定义的方法 App\Models\Article::categories()
BadMethodCallException Call to undefined method App\Models\Article::categories()
我正在处理我的 Laravel 项目,many-to-many 关系有问题
它会返回以下错误:
BadMethodCallException Call to undefined method App\Models\Article::categories()
尽管我在文章模型中将关系定义为类别,Laravel解决方案是:
错误的方法调用您是说 App\Models\Article::categories() 吗?
请帮我找出哪部分代码可能导致这个问题
文章模型:
文章管理员:
类别控制器:
Article
模型中的 categories
方法受到保护。要像您想要的那样访问它,需要将其声明为 public
.
class Article extends Model
{
public function categories()
{
return $this->belongsToMany(Category::class);
}
}
我正在处理我的 Laravel 项目,many-to-many 关系有问题
它会返回以下错误:
BadMethodCallException Call to undefined method App\Models\Article::categories()
尽管我在文章模型中将关系定义为类别,Laravel解决方案是:
错误的方法调用您是说 App\Models\Article::categories() 吗?
请帮我找出哪部分代码可能导致这个问题
文章模型:
文章管理员:
类别控制器:
Article
模型中的 categories
方法受到保护。要像您想要的那样访问它,需要将其声明为 public
.
class Article extends Model
{
public function categories()
{
return $this->belongsToMany(Category::class);
}
}