zairakai/laravel-eloquent

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

Fund package maintenance!
Patreon
Other

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Forks: 0

pkg:composer/zairakai/laravel-eloquent

v2.5.2 2026-01-19 13:11 UTC

This package is auto-updated.

Last update: 2026-01-19 12:20:22 UTC


README

Main Develop Security Coverage

GitLab Release Packagist Downloads

License

PHP Laravel Static Analysis Code Style

Enhanced Eloquent model classes and traits for automatic table management, primary key detection, JSON serialization with column mapping, and column resolution with migration support in Laravel applications.

Lightweight utility package Provides abstract classes (BaseModel, BasePivot) and trait (BaseTable) that eliminate boilerplate code when working with Eloquent models, especially for projects with custom database column naming conventions.

Installation

composer require zairakai/laravel-eloquent

Optional: Publish configuration and stubs:

php artisan vendor:publish --tag=laravel-eloquent          # Everything
php artisan vendor:publish --tag=laravel-eloquent-config   # Config only
php artisan vendor:publish --tag=laravel-eloquent-stubs    # Stubs only

Quick Start

Extend BaseModel (Recommended)

use Zairakai\LaravelEloquent\Models\BaseModel;

class User extends BaseModel
{
    public const COLUMNS = [
        'id'    => 'user_id',
        'email' => 'user_email',
        'name'  => 'full_name',
    ];

    protected $fillable = ['user_email', 'full_name'];
}

Use BaseTable Trait

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

class User extends Model
{
    use BaseTable;

    public const COLUMNS = [
        'id'    => 'user_id',
        'email' => 'user_email',
    ];
}

For Pivot Tables

use Zairakai\LaravelEloquent\Models\BasePivot;

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

    public const COLUMNS = [
        'roleId' => 'role_id',
        'userId' => 'user_id',
    ];
}

Custom Stubs

After publishing stubs, php artisan make:model uses BaseModel automatically:

php artisan vendor:publish --tag=laravel-eloquent-stubs
php artisan make:model Product
StubDescription
model.stubStandard model extending BaseModel
model.pivot.stubPivot model extending BasePivot
model.plain.stubModel with BaseTable trait only

Model Conversion

Convert existing models to use BaseModel/BasePivot:

php artisan eloquent:convert --dry-run              # Preview changes
php artisan eloquent:convert                        # Convert app/Models
php artisan eloquent:convert --path=app/Models/Legacy
php artisan eloquent:convert --force                # No confirmation

API Reference

Methods

MethodDescription
getTableName()Get the resolved table name
getPrimaryKeyName()Get the resolved primary key
resolveColumn($key)Resolve application key to DB column
toReadableArray()Convert to array with mapped keys
toJson()JSON with mapped keys

Constants

ConstantDescription
TABLE_NAMEOverride automatic table name
PRIMARY_KEYOverride primary key
COLUMNSKey → Column mapping
COLUMNS_DELETEDDeprecated key redirects (with logging)

Contributing

Code Of Conduct Contributing

Getting Help

License Security Policy Issues Discord