Thursday, March 16, 2017

Laravel getDirty and getOriginal give same values on updating

Using Laravel 5.4

I have a Job model which has a enum field on it with different statuses. These statuses change in many different places. I made a JobHistory model and migration which tracks those changes .

On my Job model i define the new laravel 5.4 way of tracking eloquent events:

/**
 * The events that should be fired for eloquent actions
 *
 * @var array
 */
protected $events = [
    'updating' => JobChangedStatus::class
];

EventServiceProvider:

'App\Events\Jobs\JobChangedStatus' => [
  'App\Listeners\Jobs\CreateJobHistory',
],

CreateJobHistory Listener:

$job = $event->job;

$jobHistory = new JobHistory();
$jobHistory->old_status = $job->getOriginal('status');
$jobHistory->new_status = $job->status;
$jobHistory->job()->associate($job);
$jobHistory->executor()->associate(Auth::user());
$jobHistory->save();

When i change my job status from e.g New to In_progress

My JobHistory table will look like this:

enter image description here

So the new_status and the old_status both give the old value. I tried using $job->getDirty() but when i print it it just gives back a empty array.

What am i doing wrong?



via Christophvh

Advertisement