Trait and Helper

Small, tested traits for everyday Eloquent work

2.0.1 GitHub ActionsMITPHP ^8.4Laravel ^12.0 | ^13.0

Reusable building blocks you'd otherwise copy between projects. ReadOnlyTrait, BulkDeleteTrait, PropertyConfigurable, HasArrayAccessTrait, AutoFillableTrait, EnumHelperTrait, and global helper functions — with strict types and tests.

Requirements

Core Package
  • PHP ^8.4
  • Laravel ^12.0 | ^13.0

Install with Composer

composer require t-labs-co/trait-and-helper

Get started in minutes

1. Install the package

composer require t-labs-co/trait-and-helper

Upgrading from v1.x? Read the Migration Guide for a step-by-step upgrade path.

No configuration file or migrations are required — the traits work immediately after installation.

2. Use a trait in your model

ReadOnlyTrait — Protect a model from accidental writes:

use Illuminate\Database\Eloquent\Model;
use TLabsCo\TraitAndHelper\Model\ReadOnlyTrait;

class Category extends Model
{
    use ReadOnlyTrait;
}

// This will throw ReadOnlyException
$category->save();

// Disable read-only on the fly
$category->disableReadOnly()->save();
$category->enableReadOnly();

BulkDeleteTrait — Delete records with transaction support and event control:

use Illuminate\Database\Eloquent\Model;
use TLabsCo\TraitAndHelper\Model\BulkDeleteTrait;

class Category extends Model
{
    use BulkDeleteTrait;
}

// Delete by query builder
Category::deletes(Category::where('name', 'unknown'));

// Delete single model
Category::deletes($category);

// Force delete (for soft-deletable models)
Category::forceDeletes(Category::where('name', 'unknown'));

// Delete without firing events
Category::deletesQuietly(Category::where('name', 'unknown'));

PropertyConfigurable — Add cached, dynamic settings to any model:

use Illuminate\Database\Eloquent\Model;
use TLabsCo\TraitAndHelper\Model\PropertyConfigurable;

class Product extends Model
{
    use PropertyConfigurable;

    protected $fillable = ['name', 'settings', 'extras'];
    protected $casts = ['settings' => 'array', 'extras' => 'array'];
    protected $propertyConfigKey = 'settings';
}

// Access configuration
$product = Product::find(1);
$config = $product->config();
$value = $config->get('key');

// Update configuration
$product->config()->set('key', 'new value');
$product->save();

HasArrayAccessTrait — Array-like access to object properties:

use TLabsCo\TraitAndHelper\Arrays\HasArrayAccessTrait;

class DataObject
{
    use HasArrayAccessTrait;

    protected array $options = [];

    protected function arrayDataKey(): string
    {
        return 'options';
    }
}

$dataObject = new DataObject(['name' => 'John']);
$dataObject->get('name');       // 'John'
$dataObject->set('age', 30);
$dataObject['email'] = 'test@example.com';  // ArrayAccess

AutoFillableTrait — Auto-populate $fillable from database columns:

use Illuminate\Database\Eloquent\Model;
use TLabsCo\TraitAndHelper\Model\AutoFillableTrait;

class Category extends Model
{
    use AutoFillableTrait;
    // No need to manually define $fillable — it's auto-populated from table columns
}

EnumHelperTrait — Helper methods for PHP backed enums:

use TLabsCo\TraitAndHelper\Enum\EnumHelperTrait;

enum Status: string
{
    use EnumHelperTrait;

    case Active = 'active';
    case Inactive = 'inactive';
}

Status::labels();   // ['Active', 'Inactive']
Status::values();   // ['active', 'inactive']
Status::toSelect(); // [['active' => 'Active'], ['inactive' => 'Inactive']]
Status::Active->label();  // 'Active'

3. Helper functions

camel2dashed('camelCase');           // 'camel-case'
camel2dashed('camelCase', '_');      // 'camel_case'
class2camel('App\Models\User');      // 'user'
class_name_only('App\Enums\Status'); // 'status'
has_use(SomeTrait::class, $obj);     // true/false
parse_date_carbon('2024-01-01');     // Carbon instance

Features

ReadOnlyTrait

Prevents accidental writes on models that should never be modified after creation. Supports toggling read-only mode on the fly with disableReadOnly() and enableReadOnly().

BulkDeleteTrait

Provides transactional bulk deletes with event control. Methods include deletes(), forceDeletes(), and deletesQuietly() for different deletion scenarios.

PropertyConfigurable

Gives you cached, dynamic settings stored in JSON attributes. Access via $model->config()->get('key') and update with $model->config()->set('key', 'value').

HasArrayAccessTrait

Array-like access to object properties via magic methods and the ArrayAccess interface. Supports get(), set(), has(), and direct array access.

AutoFillableTrait

Automatically populates mass-assignment $fillable attributes from database table columns, eliminating manual maintenance.

EnumHelperTrait

Provides helper methods for PHP 8.1+ backed enums: labels(), values(), names(), toArray(), toSelect(), toList(), and instance methods like label(), toUpper(), toLower().

Helper Functions

Global functions for string conversion, class introspection, date parsing, and trait detection.


When to use each trait

Trait Use case
ReadOnlyTrait Audit logs, invoices, historical records that must never change
BulkDeleteTrait Cleaning up large tables where model events matter
PropertyConfigurable Models with dynamic settings, preferences, or feature flags
HasArrayAccessTrait Objects that need array-like property access
AutoFillableTrait Models where you don’t want to maintain $fillable manually
EnumHelperTrait Enums that need labels, select arrays, or list formatting

For complete documentation, see the README on GitHub.

Why I built it

I found myself copying the same traits across every Laravel project — read-only guards, bulk deletes with events, configurable properties. Instead of maintaining private forks, I packaged them here with proper tests and type safety so any project can benefit.

Hire me for Laravel Eloquent and reusable component work

Need a package tailored to your application? From recurring Laravel maintenance tasks to reusable components, I can help design, build, and review production-ready solutions.

Discuss your project