定制 zairakai/laravel-eloquent 二次开发

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

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

zairakai/laravel-eloquent

Composer 安装命令:

composer require zairakai/laravel-eloquent

包简介

Enhanced Eloquent model trait for automatic table management, primary key detection, and JSON serialization with column mapping

README 文档

README

Main Develop Coverage

GitLab Release Packagist Downloads License

PHP Laravel Static Analysis Code Style

Eloquent base classes and helpers for safer column mapping, automatic table detection, and clean JSON serialization.

Features

  • Column mapping — map logical names to physical database column names via a COLUMNS constant
  • Relation mapping — map logical names to Eloquent relationship methods via a RELATIONS constant
  • Scope mapping — map logical names to Eloquent scope methods via a SCOPES constant
  • Automatic table detection — derives table name from class name and namespace, no configuration needed
  • Primary key detection — resolves from PRIMARY_KEY constant or COLUMNS['id'], defaults to 'id'
  • Deprecated column tracking — redirect renamed columns via COLUMNS_DELETED with automatic log warnings
  • Safe JSON serializationtoJson(), jsonSerialize(), and toReadableArray() use logical column names
  • Transparent Eloquent APIfill(), getAttribute(), setAttribute(), isFillable() resolve column names automatically
  • BaseModel — ready-to-extend abstract model with all features pre-configured
  • BasePivot — ready-to-extend abstract pivot class, non-incrementing by default
  • BaseTable trait — use all features in any existing model without changing its base class
  • Configurable logging — channel, level, backtrace depth, and per-model exclusions via config/laravel-eloquent.php
  • eloquent:convert command — detect and convert existing Model/Pivot classes to BaseModel/BasePivot
  • Published stubsmodel.stub, model.pivot.stub, model.plain.stub for make:model

Install

composer require zairakai/laravel-eloquent

Usage

Extend BaseModel

use Zairakai\LaravelEloquent\Models\BaseModel;

class User extends BaseModel
{
    public const string ROUTE_PARAM = 'user';

    public const array COLUMNS = [
        'id'    => 'user_id',
        'email' => 'user_email',
        'name'  => 'full_name',
    ];

    public const array RELATIONS = [
        'roles' => 'roles',
    ];

    public const array SCOPES = [
        'suspended' => 'scopeSuspended',
    ];
}

// Eloquent methods use logical names transparently
User::where('email', 'alice@example.com')->first();
$user->fill(['name' => 'Alice']);
$user->getAttribute('email');

// Resolution helpers
User::resolveColumn('email');    // 'user_email'
User::resolveRelation('roles');  // 'roles'
User::resolveScope('suspended'); // 'scopeSuspended'

Extend BasePivot

use Zairakai\LaravelEloquent\Models\BasePivot;

class RoleUser extends BasePivot
{
    public const TABLE_NAME = 'role_user';

    public const COLUMNS = [
        'role_id' => 'fk_role',
        'user_id' => 'fk_user',
    ];
}

BasePivot is non-incrementing by default ($incrementing = false).

Use the trait on an existing model

use Illuminate\Database\Eloquent\Model;
use Zairakai\LaravelEloquent\Traits\BaseTable;

class Post extends Model
{
    use BaseTable;

    public const COLUMNS = [
        'id'    => 'post_id',
        'title' => 'post_title',
    ];
}

Table name resolution

The table name is derived automatically. You can override it with TABLE_NAME:

// App\Models\User → users
// App\Models\BlogPost → blog_posts
// App\Models\Shop\Product → shop_products  (namespace prefix)

class Invoice extends BaseModel
{
    public const TABLE_NAME = 'billing_invoices'; // explicit override
}

Primary key resolution

Resolution order: PRIMARY_KEY constant → COLUMNS['id'] value → 'id' fallback.

class Order extends BaseModel
{
    public const PRIMARY_KEY = 'order_uuid';
}

class Product extends BaseModel
{
    public const COLUMNS = [
        'id' => 'product_id', // resolved as primary key
    ];
}

Deprecated column tracking

Rename a column in COLUMNS and keep the old key in COLUMNS_DELETED to redirect legacy code with a log warning instead of silently breaking:

class User extends BaseModel
{
    public const COLUMNS = [
        'id'       => 'user_id',
        'username' => 'login_name', // renamed column
    ];

    public const COLUMNS_DELETED = [
        'login' => 'username', // 'login' → redirects to 'username' + logs a warning
    ];
}

$user->getAttribute('login'); // resolves to 'login_name', logs deprecation warning

Publish configuration

php artisan vendor:publish --tag=laravel-eloquent-config

Key options in config/laravel-eloquent.php:

KeyDefaultDescription
logging.enabledtrueEnable/disable all column resolution logging.
logging.channelnullLog channel (uses default Laravel channel if null).
logging.levels.deprecated'warning'Log level for deprecated column access.
logging.levels.missing'info'Log level for columns not found in COLUMNS.
logging.include_backtracetrueInclude call backtrace in log entries.
logging.backtrace_depth5Number of stack frames in the backtrace.
logging.excluded_models[]Model classes excluded from logging.

Publish stubs

php artisan vendor:publish --tag=laravel-eloquent-stubs

Published stubs: stubs/model.stub, stubs/model.pivot.stub, stubs/model.plain.stub.

Convert existing models

Detect and convert all Model / Pivot classes in your app/Models directory:

# Preview changes without modifying files
php artisan eloquent:convert --dry-run

# Convert with confirmation prompt
php artisan eloquent:convert

# Convert a custom path without confirmation
php artisan eloquent:convert --path=app/Domain/Models --force

The command replaces extends Model with extends BaseModel and extends Pivot with extends BasePivot, updates imports, and removes any manual use BaseTable statements.

Development

make quality        # pint + phpstan + rector + insights + markdownlint + shellcheck
make quality-fast   # pint + phpstan + markdownlint
make test           # phpunit / pest

Contributing

Contributions are welcome. Please read CONTRIBUTING.md for the project-specific workflow and quality standards.

Getting Help

License Security Policy Issues

Made with ❤️ by Zairakai

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

zairakai/laravel-eloquent 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 41 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 41
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 44
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-11