-2

This is my function. and i'm getting error in it.Kindly help me solve this error.and also tell me why I'm getting this error.

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

3 Answers3

0

Create an instance first. But I don't think that's the real issue.

$newGrade = (new Grade() )->update($grade);

return [
    'status' => 'success',
    'grade'  => $newGrade,
];

Try changing your method to 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,
    ];
jeremykenedy
  • 4,150
  • 1
  • 17
  • 25
0

Instantiating a new class

You are trying to access the class as if it were a Facade. You could possibly change the use statement at the top to prepend Facade. The Laravel way of instantiating the class would either be through dependancy injection where you pass the class to the method parameters

public function update(Grade $grade, Request $request) {
    // do stuff with $grade->update()
}

or use Real Time Facades or use it the way JeremyKenedy suggested but by using the app() helper function.

app()->make(Grade::class)
Thinus
  • 11
  • 3
0

Your update function is wrong. please try it.

  public function update(Request $request)
    {
        $id = $request->id;
        $grade = Grade::find($id);

        $grade = $request->validate([
            'title' => 'required|string',
            'slig' => 'string',
            'description' => 'string',
        ]);

        $grade->update([ 
           'title' => $request->title,
           'slig' => $request->slig,
           'description' => $request->description
        ]);

        return [
            'staus' => 'success',
            'grade' => $grade,
        ];
    } 
Rashiqul Rony
  • 79
  • 1
  • 7