t-labs-co / trait-and-helper
Collection of traits and helpers for Laravel.
Fund package maintenance!
ty-huynh
Requires
- php: ^8.4||^8.3||^8.2
- illuminate/contracts: ^10.0||^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
- spatie/laravel-ray: ^1.35
This package is not auto-updated.
Last update: 2025-03-23 06:58:52 UTC
README
This package provides a collection of useful traits and helper functions to enhance your Laravel applications. It includes traits for array access, bulk deletion, and more, making it easier to manage common tasks in your projects.
Work with us
We're PHP and Laravel whizzes, and we'd love to work with you! We can:
- Design the perfect fit solution for your app.
- Make your code cleaner and faster.
- Refactoring and Optimize performance.
- Ensure Laravel best practices are followed.
- Provide expert Laravel support.
- Review code and Quality Assurance.
- Offer team and project leadership.
- Delivery Manager
Features
HasArrayAccessTrait
: Provides array-like access to object properties.ReadOnlyTrait
: Protects models from being modifiedBulkDeleteTrait
: Simplifies bulk deletion of Eloquent models with transaction support.EnumHelperTrait
: Provides helper methods for working with PHP enums.- Additional helper functions to streamline common tasks.
PHP and Laravel Version Support
This package supports the following versions of PHP and Laravel:
- PHP:
^8.2
- Laravel:
^11.0
,^12.0
Installation
You can install the package via composer:
composer require t-labs-co/trait-and-helper
Usage
### # HasArrayAccessTrait ### class DataObject implements \ArrayAccess { use HasArrayAccessTrait; protected $options; public function __construct($initData = []) { $this->options = $initData; } protected function arrayDataKey() { return 'options'; } } // using DataObject->get($key); DataObject->set($key, $value); DataObject->has($key); ### # ReadOnlyTrait # This simply protection for your read model ### class Category extends Model { use HasFactory; use ReadOnlyTrait; // ... } // using // if you get $category->save(); // this will throw an exception: Can not save the read-only model Category // We can enable or disable read only on the fly $category->disableReadOnly()->save(); ### # BulkDeleteTrait # this simply using transaction and make delete model with event trigger ### class Category extends Model { use HasFactory; use BulkDeleteTrait; //... } // Using simple delete Category::deletes(Category::where('name', 'unknown')); Category::deletes($category); Category::deletes($categoryCollection); Category::deletes(Category::where('name', 'unknown')->get()); // Force delete Category::forceDeletes(Category::where('name', 'unknown')); // delete quitely Category::deletesQuietly(Category::where('name', 'unknown'));
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
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.