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

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

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

stayallive/laravel-eloquent-observable

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.

stayallive/laravel-eloquent-observable 适用场景与选型建议

stayallive/laravel-eloquent-observable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40.92k 次下载、GitHub Stars 达 29, 最近一次更新时间为 2022 年 02 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「eloquent」 「observers」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 stayallive/laravel-eloquent-observable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 stayallive/laravel-eloquent-observable 我们能提供哪些服务?
定制开发 / 二次开发

基于 stayallive/laravel-eloquent-observable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-28