方法类别不存在。 laravel

时间:2018-06-17 22:25:54

标签: laravel-5.4

这是什么错误?

  

方法类别不存在。 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);
}

1 个答案:

答案 0 :(得分:0)

您无法在请求中访问categories关系。您需要将其添加到模型中。将代码更改为:

$article = auth()->user()->article()->create(array_merge($request->all(), ['images' => $imagesUrl]));
$article->categories()->attach(request('category'));