composer require sme/laravel-model-life-events
class Posts extends Model
{
public static function booted() {
self::deleted(function (self $model) {
// listening to the deletion event in the current model
...
});
}
For this code, the listener will not work because the method calling the deleted event is missing from the Builder class
public static function deletePost(int $post_id) {
return self::where('id', $post_id)->delete();
}
public static function deletePost(int $post_id) {
$post = self::find($post_id);
if ($post) {
return $post->delete();
}
}
To use the delete or update methods without additional first, find, etc. methods. You can use trait "HasEvents" in your models class
use SME\Laravel\Model\HasEvents;
class Posts extends Model
{
use HasEvents;
...