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
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
COLUMNSconstant - Relation mapping — map logical names to Eloquent relationship methods via a
RELATIONSconstant - Scope mapping — map logical names to Eloquent scope methods via a
SCOPESconstant - Automatic table detection — derives table name from class name and namespace, no configuration needed
- Primary key detection — resolves from
PRIMARY_KEYconstant orCOLUMNS['id'], defaults to'id' - Deprecated column tracking — redirect renamed columns via
COLUMNS_DELETEDwith automatic log warnings - Safe JSON serialization —
toJson(),jsonSerialize(), andtoReadableArray()use logical column names - Transparent Eloquent API —
fill(),getAttribute(),setAttribute(),isFillable()resolve column names automatically BaseModel— ready-to-extend abstract model with all features pre-configuredBasePivot— ready-to-extend abstract pivot class, non-incrementing by defaultBaseTabletrait — 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:convertcommand — detect and convert existingModel/Pivotclasses toBaseModel/BasePivot- Published stubs —
model.stub,model.pivot.stub,model.plain.stubformake: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:
| Key | Default | Description |
|---|---|---|
logging.enabled | true | Enable/disable all column resolution logging. |
logging.channel | null | Log 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_backtrace | true | Include call backtrace in log entries. |
logging.backtrace_depth | 5 | Number 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
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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zairakai/laravel-eloquent 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Fork of jms/serializer 1.14.1 with support for modern PHP versions.
De/normalize business objects without tightly coupling them to your normalization format
Wordpress Query Builder class library for custom models and data querying.
Runn Me! Serialization Library
A Laravel package for managing model drafts.
A package to better handle database operations via Eloquent Models.
统计信息
- 总下载量: 41
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 44
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-11