Laravel 8: Method Illuminate\Database\Eloquent\Collection::update does not exist ERROR

tejoslaeslio

I want to update some data from the DB, so I added this Controller method:

public function updateAnswer(Answer $anss)
    {
        $validate_data = Validator::make(request()->all(),[
           'answer' => 'required'
        ])->validated();

        $answer = Answer::findOrFail($anss);
        $answer->update($validate_data);
        return back();
    }

Now the problem is I get this error:

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

So how to solve this issue?

Kurt Friars

You are already resolving $anss using route-model binding.

public function updateAnswer(Answer $anss)

You are trying to call findOrFail with a model as an argument, which since Model implements Arrayable will return a Collection, thus breaking the update call.

See Illuminate\Database\Eloquent\Builder findOrFail -> find -> findMany -> return $this->whereKey($ids)->get($columns);.

Try:

    public function updateAnswer(Answer $anss)
    {
        $validate_data = Validator::make(request()->all(),[
           'answer' => 'required'
        ])->validated();

        $anss->update($validate_data);

        return back();
    }

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

Laravel Method [where] does not exist error

Laravel Method notify does not exist

Laravel Update Method Error

Laravel - Collection::delete method does not exist

laravel pagination Method links does not exist

Method orderBy does not exist in Laravel Eloquent?

Laravel eloquent issue: Method does not exist

Laravel 5.2 - Method links does not exist

Laravel Eloquent collection method does not exist

Laravel - Method [orderBy] does not exist on Axois Request

Laravel 8. Seeder class does not exist

Laravel 8: Target class [ProductController] does not exist

Laravel Method Illuminate\Database\Eloquent\Collection::toSql does not exist. error

Paypal error - getTransactionFee method does not exist

"ReflectionException: Class config does not exist" on update to Laravel 5.7

Laravel - Como resolver o erro Method paginate does not exist

Laravel - BadMethodCallException: Method Illuminate\Validation\Validator::validatePatternName does not exist

Target class does not exist. problem in laravel 8

builder::sync does not exist error with many-to-many in Laravel

Update an object or create it if does not exist

BadMethodCallException: Method year does not exist

Laravel resource routing - update throws "method not allowed" error

Laravel 8 error route | The GET method is not supported for this route

Laravel 8 Routing Update?

PSQL Error: function does not exist

Postgresql Error type " " does not exist

Laravel 6 Error - Illuminate\Contracts\Container\BindingResolutionException Target class does not exist

Laravel : Update field when value is exist

Insert if not exist and update certain values if it does

TOP lista

quentelabel

Arquivo