search

Home  >  Q&A  >  body text

How to solve the error of statically calling non-static method 'update'?

<p>This is my function. I am getting an error in it, please help me to resolve this error and tell me why this error is appearing. </p> <pre class="brush:php;toolbar:false;">public function update(Request $request) { $id = $request->id; $grade = Grade::find($id); $grade = $request->validate([ 'title' => 'required|string', 'slig' => 'string', 'description' => 'string', ]); $grade = Grade::update($grade); return [ 'staus' => 'success', 'grade' => $grade, ]; }</pre>
P粉032900484P粉032900484465 days ago576

reply all(1)I'll reply

  • P粉156983446

    P粉1569834462023-08-26 00:38:39

    First create an instance. But I don't think that's the real issue.

    $newGrade = (new Grade())->update($grade);
    
    return [
        'status' => 'success',
        'grade'  => $newGrade,
    ];

    Try changing your method as follows:

    $grade = Grade::findOrFail($request->id);
    
    $validatedData = $request->validate([
        'title' => 'required|string',
        'slig' => 'string',
        'description' => 'string',
    ]);
    
    $grade->update($validatedData);
    
    return [
        'status' => 'success',
        'grade' => $grade,
    ];

    reply
    0
  • Cancelreply