定制 stayallive/laravel-eloquent-observable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

stayallive/laravel-eloquent-observable

最新稳定版本:v1.0.4

Composer 安装命令:

composer require stayallive/laravel-eloquent-observable

包简介

Register Eloquent model event listeners just-in-time directly from the model.

README 文档

README

Latest Version Software License Build Status Total Downloads

Register Eloquent model event listeners just-in-time directly from the model.

Using Observers can introduce a (significant) overhead on the application since they are usually registered in a service provider which results in every model in your application with a observer is "booted" a startup of the application even though the model is never touched in the request. This package aims to reduce that overhead by connecting listeners just-in-time whenever the Eloquent model is booted (first used) in the request. The event callbacks are also defined on the model itself keeping the code cleaner, althought this is my preference of course and if you disagree this might not be the package for you.

Note

Laravel 10.44 introduced the ObservedBy attribute which allows you to define the observer on the model itself which negates all the performance benefits of this package. However this package still provides an alternative way to define the event handler methods on the model itself so might still be considered useful but using the ObservedBy attribute will solve the performance issue with registering the model observers inside a service provider.

Installation

composer require stayallive/laravel-eloquent-observable

Usage

Adding the Observable trait will ensure that the observable events are connected to the event handlers defined on the model.

<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Stayallive\Laravel\Eloquent\Observable\Observable; class SomeModel extends Model { use Observable; // Event handlers are defined by `onEventName` where `EventName` is any valid Eloquent event (or custom event) // See a full list of Eloquent events: https://laravel.com/docs/9.x/eloquent#events public static function onSaving(self $model): void { // For example: $model->slug = str_slug($model->title); } }

Security Vulnerabilities

If you discover a security vulnerability within this package, please send an e-mail to Alex Bouma at alex+security@bouma.me. All security vulnerabilities will be swiftly addressed.

License

This package is open-sourced software licensed under the MIT license.

统计信息

  • 总下载量: 26.6k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 29
  • 点击次数: 0
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 29
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固