这是什么错误?
方法类别不存在。 laravel
ArticleController.php
public function store(ArticleRequest $request)
{
$imagesUrl = $this->uploadImages($request->file('images'));
auth()->user()->article()->create(array_merge($request->all(), ['images' => $imagesUrl]));
$request->categories()->attach(request('category'));
return redirect(route('articles.index'));
}
Article.php
public function categories()
{
return $this->belongsToMany(Category::class);
}
Category.php
public function articles()
{
return $this->belongsToMany(Article::class);
}
答案 0 :(得分:0)
您无法在请求中访问categories
关系。您需要将其添加到模型中。将代码更改为:
$article = auth()->user()->article()->create(array_merge($request->all(), ['images' => $imagesUrl]));
$article->categories()->attach(request('category'));