Are there callbacks in Laravel like:
afterSave() beforeSave() etc
I searched but found nothing. If there are no such things - what is best way to implement it?
Thanks!
The best way to achieve before and after save callbacks in to extend the save()
function.
Here's a quick example
class Page extends Eloquent { public function save(array $options = []) { // before save code parent::save($options); // after save code } }
So now when you save a Page object its save()
function get called which includes the parent::save()
function;
$page = new Page; $page->title = 'My Title'; $page->save();
Adding in an example for Laravel 4:
class Page extends Eloquent { public static function boot() { parent::boot(); static::creating(function($page) { // do stuff }); static::updating(function($page) { // do stuff }); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With