Laravel Observer or MySQL Trigger?

Viewed 860

i'm feeling confused, which are best practices between laravel observer or MySQL Trigger.

In Laravel, My Code looks like

public function updated(My_Activity $my_activity)
{
    $activity = new Activity_Log();
    $activity->activity_id = $my_activity->id;
    $activity->status = $my_activity->status;
    $activity->description = $my_activity->description;
    $activity->save();
}

In MySQL,

BEGIN
INSERT INTO Activity_Log
SET id = OLD.id, status = OLD.status, description = OLD.description
END

What is the best practice ? Is there a good impact for one of them in the future?

1 Answers

I prefer Laravel Observer option because it allows you to keep your business logic in your application (source control). Additionally you keep your business logic at the same abstraction level by using Eloquent.

For the same reason Laravel introduced a Task scheduler. It allows you to keep your cron entries under source control.

https://laravel.com/docs/5.5/scheduling

In the past, you may have generated a Cron entry for each task you needed to schedule on your server. However, this can quickly become a pain, because your task schedule is no longer in source control and you must SSH into your server to add additional Cron entries.

Related