t-labs-co / trait-and-helper
Collection of reusable traits and helper utilities for Laravel — model protection, bulk operations, enum helpers, array-access, dynamic property configuration, and more.
Fund package maintenance!
Requires
- php: ^8.4
- illuminate/contracts: ^12|^13
- illuminate/database: ^12|^13
- illuminate/support: ^12|^13
Requires (Dev)
- larastan/larastan: ^3
- laravel/pint: ^1
- orchestra/testbench: ^10|^11
- pestphp/pest: ^4|^5
- pestphp/pest-plugin-arch: ^3|^4|^5
- pestphp/pest-plugin-laravel: ^4|^5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Collection of reusable traits and helper utilities for Laravel — model protection, bulk operations, enum helpers, array-access, dynamic property configuration, and more.
Features
- HasArrayAccessTrait: Provides array-like access to object properties via magic methods and ArrayAccess interface.
- ReadOnlyTrait: Protects Eloquent models from being saved, updated, or deleted.
- BulkDeleteTrait: Simplifies bulk deletion of Eloquent models with transaction support and event control.
- AutoFillableTrait: Automatically populates mass-assignment fillable attributes from database table columns.
- EnumHelperTrait: Provides helper methods for working with PHP 8.1+ backed enums.
- PropertyConfigurable: Manage dynamic configurations stored in model attributes like
settingsorextras. - Helper Functions: Global functions for string conversion, class introspection, date parsing, and trait detection.
Requirements
- PHP:
^8.4 - Laravel:
^12.0|^13.0
Installation
You can install the package via composer:
composer require t-labs-co/trait-and-helper
Upgrading from v1.x? Read the Migration Guide for a step-by-step upgrade path.
Usage
HasArrayAccessTrait
Provides array-like access to object properties via magic methods and the ArrayAccess interface.
<?php declare(strict_types=1); namespace App\Models; use TLabsCo\TraitAndHelper\Arrays\HasArrayAccessTrait; class DataObject { use HasArrayAccessTrait; protected array $options = []; public function __construct(array $initData = []) { $this->options = $initData; } protected function arrayDataKey(): string { return 'options'; } } // Usage $dataObject = new DataObject(['name' => 'John']); $dataObject->get('name'); // 'John' $dataObject->set('age', 30); $dataObject->has('name'); // true $dataObject['email'] = 'test@example.com'; // ArrayAccess
ReadOnlyTrait
Protects Eloquent models from being saved, updated, or deleted.
<?php declare(strict_types=1); namespace App\Models; 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
Simplifies bulk deletion of Eloquent models with transaction support.
<?php declare(strict_types=1); namespace App\Models; 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); // Delete collection Category::deletes($categoryCollection); // Force delete (for soft-deletable models) Category::forceDeletes(Category::where('name', 'unknown')); // Delete without firing events Category::deletesQuietly(Category::where('name', 'unknown'));
AutoFillableTrait
Automatically populates mass-assignment fillable attributes from database table columns.
<?php declare(strict_types=1); namespace App\Models; 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 } $category = new Category(); $category->fill($data)->save();
PropertyConfigurable
Manage dynamic configurations stored in model attributes like settings or extras.
<?php declare(strict_types=1); namespace App\Models; 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(); // Clear cached configuration $product->clearConfigCached();
EnumHelperTrait
Provides helper methods for working with PHP 8.1+ backed enums.
<?php declare(strict_types=1); namespace App\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::names(); // ['Active', 'Inactive'] Status::toArray(); // ['active', 'inactive'] Status::toSelect(); // [['active' => 'Active'], ['inactive' => 'Inactive']] Status::toList(); // 'active,inactive' Status::Active->label(); // 'Active' Status::Active->toUpper(); // 'ACTIVE' Status::Active->toLower(); // 'active' Status::Active->is('active'); // true
Helper Functions
The package provides several global 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
Quality & Development Practices
This package follows strict development standards:
- PHPStan Level 8: Static analysis with Larastan for type safety and bug prevention.
- Pest PHP: Modern testing framework with parallel execution support.
- Laravel Pint: Code style enforcement following Laravel conventions.
- Full Type Coverage: All parameters, properties, and return types are explicitly typed.
- Strict Types: Every file uses
declare(strict_types=1).
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.