2
votes

С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;
    }
1
Are you sure it’s this code throwing the error? Maybe the logic in the redirected page throws the error? This code looks okay to me...David Heremans
it redirect successfully when i remove $post->update($data);Chairuman
why do you have this "Requests\PostRequest" instead of Request?Emeka Okafor
to validate inputChairuman
You problem is with this line $post->update($data)CodeBoyCode

1 Answers

3
votes

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
}