getupkid/laravel-eloquent-casts

Named constants and builders for Laravel Eloquent casts, avoiding magic strings in model cast definitions.

Maintainers

Package info

github.com/getupkid/laravel-eloquent-casts

pkg:composer/getupkid/laravel-eloquent-casts

Transparency log

Statistics

Installs: 121

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.1.0 2026-08-13 13:55 UTC

This package is auto-updated.

Last update: 2026-08-14 10:40:27 UTC


README

Named constants and builders for Laravel's string-based Eloquent casts. The package replaces cast magic strings such as 'boolean', 'collection', and 'decimal:2' with discoverable, reusable references such as Cast::BOOLEAN, Cast::COLLECTION, and Cast::decimal(2).

This keeps model cast definitions easier to discover through IDE completion, reduces typographical errors, and provides one consistent place to reference Laravel's supported primitive cast values.

Installation

composer require getupkid/laravel-eloquent-casts

Usage

Laravel normally defines primitive casts using strings:

protected function casts(): array
{
    return [
        'is_active' => 'boolean',
        'settings' => 'json',
        'price' => 'decimal:2',
    ];
}

With this package, those magic strings become named constants or builder methods:

<?php

use Getupkid\LaravelEloquentCasts\Cast;

protected function casts(): array
{
    return [
        'is_active' => Cast::BOOLEAN,
        'settings' => Cast::JSON,
        'price' => Cast::decimal(2),
        'published_at' => Cast::datetime('Y-m-d H:i:s'),
    ];
}

The package covers Laravel's string cast values:

  • ARRAY, BOOL, BOOLEAN, COLLECTION
  • DATE, DATETIME, IMMUTABLE_DATE, IMMUTABLE_DATETIME, TIMESTAMP
  • DOUBLE, FLOAT, REAL, INT, INTEGER, STRING
  • JSON, JSON_UNICODE, OBJECT
  • ENCRYPTED, ENCRYPTED_ARRAY, ENCRYPTED_COLLECTION, ENCRYPTED_JSON, ENCRYPTED_OBJECT
  • HASHED

Parameterised casts are available through decimal(), date(), datetime(), immutableDate(), and immutableDatetime().

Class-based and enum casts

This package represents Laravel's built-in string-based cast identifiers. It does not wrap enum casts, Laravel cast classes, or your application's custom cast classes.

Those casts already have a type-safe, discoverable identifier: their PHP class name. Continue to reference them with ::class alongside this package's constants:

<?php

use App\Casts\Money;
use App\Enums\UserStatus;
use Getupkid\LaravelEloquentCasts\Cast;
use Illuminate\Database\Eloquent\Casts\AsStringable;

protected function casts(): array
{
    return [
        'is_active' => Cast::BOOLEAN,
        'status' => UserStatus::class,
        'reference' => AsStringable::class,
        'asking_price' => Money::class,
    ];
}

UserStatus::class, AsStringable::class, and Money::class are not magic strings: PHP and your IDE can resolve those symbols, follow renames, and report missing classes. Wrapping them in another constant would add indirection without improving type safety.

See Laravel's documentation for enum casting and custom casts.

Testing

composer test

Licence

MIT