方法Illuminate \ Database \ Eloquent \ Collection :: update不存在

时间:2019-02-09 20:14:15

标签: php laravel

有人可以帮助我解决此错误吗?当我尝试更新帖子时会发生这种情况, 这是我的更新功能

public function update(Requests\PostRequest $request, $id)
    {
        $post = Post::findOrFail($id);
        $data = $this->handleRequest($request);
        $post->update($data);
        return redirect('/blog/post')->with('message','Your posts was updated successfully');
    }

这是我的handleRequest函数

private function handleRequest($request)
    {
        $data = $request->all();

        if ($request->hasFile('image')) {
            $image = $request->file('image');
            $fileName = $image->getClientOriginalName();
            $destination = $this->uploadPath;

            $image->move($destination, $fileName);

            $data['image'] = $fileName;
        }

        return $data;
    }

1 个答案:

答案 0 :(得分:0)

您正在访问collection实例,需要访问基础模型:

foreach($post as $p){//loop through an Eloquent collection instance
    $p->fill($data);//mass assign the new values
    $p->save();//save the instance
}