vendredi 8 novembre 2019

Laravel: Observer is not working on update [duplicate]

This question is an exact duplicate of:

i am trying to check if any field is changed when updating by model observer. i have registered observer by following Laravel documentation, but nothing seems to work.

TeacherObserver:

<?php

namespace App\Observers;
use App\Teacher;
use DB;
use Auth;
class TeacherObserver
{
public function updated(Teacher $teacher)
    {

         $dirty = $teacher->getDirty();

               if (count($dirty) < 1)
               {
                 return response()->json([
                   'msg' => 'Error: Type something new to Update',
                 ]);
               }
    }

}

UpdateController:

public function update(TeacherRequest $request, $id)
{
$teacher = Teacher::find($id);
if($teacher->save()){    
$teacher->update($request->all());
  return response()->json([
  'msg' => 'Record Has Been Updated',
  ]);
}
}

i have tried composer dump, cache clear. i have registered observer in AppServiceProvider boot().

public function boot()
    {
        Teacher::observe(TeacherObserver::class);
    }

For now i just want to check if no field is changed, then i should get response from observer. right now only record is updating, observer is not checking anything.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire