This package provides a collection of useful traits and helper functions to enhance your Laravel applications
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.
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
HasArrayAccessTrait
: Provides array-like access to object properties.ReadOnlyTrait
: Protects models from being modifiedBulkDeleteTrait
: Simplifies bulk deletion of Eloquent models with transaction support.AutoFillableTrait
: Automatically with mass assignment only attributes from table data.EnumHelperTrait
: Provides helper methods for working with PHP enums.- Additional helper functions to streamline common tasks.
This package supports the following versions of PHP and Laravel:
- PHP:
^8.2
- Laravel:
^11.0
,^12.0
You can install the package via composer:
composer require t-labs-co/trait-and-helper
###
# HasArrayAccessTrait
###
class DataObject implements \ArrayAccess
{
use HasArrayAccessTrait;
protected $options;
public function __construct($initData = [])
{
$this->options = $initData;
}
protected function arrayDataKey()
{
return 'options';
}
}
// using
$dataObject = new DataObject();
$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'));
###
# AutoFillableTrait
# this trait automatically popular mass assignment only attributes from table columns.
###
class Category extends Model
{
use HasFactory;
use AutoFillableTrait;
//...
// this model don't need to config $fillable attributes it will auto fetch from database table and perform mass-assignment
}
// using
$category = new Category();
$category->fill($data); //
$category->save();
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.