Chairuman
Chairuman

Reputation: 136

Method Illuminate\Database\Eloquent\Collection::update does not exist

Сan anybody help me with this error? It happens when I try to update post, here's my update function

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');
    }

and here's my handleRequest function

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;
    }

Upvotes: 3

Views: 9765

Answers (1)

user3647971
user3647971

Reputation: 1056

You are accessing a collection instance, you need to access the underlying model:

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

Upvotes: 4

Related Questions