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.PropertyConfigurable
: Alows you to manage dynamic configurations stored in attributes like settings or extras. It provides methods for accessing, updating, and caching configuration values, making it easy to work with flexible data structures in your models.- 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();
This trait allows you to manage dynamic configurations stored in attributes like settings or extras. It provides methods for accessing, updating, and caching configuration values, making it easy to work with flexible data structures in your models.
Add using trait PropertyConfigurable
//
class Product extends Model
{
use PropertyConfigurable;
// Make `settings` and `extras` fillable
protected $fillable = [
'name',
'settings',
'extras',
];
// Add `settings` and `extras` to casts
protected $casts = [
'settings' => 'array',
'extras' => 'array',
];
// Optionally, override the property config key
protected $propertyConfigKey = 'settings';
// Or define a custom config key getter
protected function getPropertyConfigKey()
{
return 'settings';
}
}
Using the config in application
```php
$product = Product::find(1);
// Access the `settings` configuration
$config = $product->config();
$value = $config->get('key'); // Get a specific key from settings
You can update the configuration values dynamically.
$product = Product::find(1);
// Update a specific key in the `settings` configuration
$product->config()->set('key', 'new value');
// Save the changes
$product->save();
Clearing Cached Configurations
// You can clear the cached configuration values if needed.
$product = Product::find(1);
// Clear the cached configuration
$product->clearConfigCached();
Using a Custom Config Attribute
class Product extends Model
{
use PropertyConfigurable;
protected $fillable = ['name', 'settings', 'extras'];
protected $casts = ['settings' => 'array', 'extras' => 'array'];
public function getExtrasAttribute()
{
return $this->config()->makePropertyConfig($this, 'extras');
}
}
// Access the `extras` configuration
$product = Product::find(1);
$extras = $product->extras->get('extra_key');
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.