t-labs-co/trait-and-helper

Collection of traits and helpers for Laravel.

1.0.0 2025-03-22 08:43 UTC

This package is not auto-updated.

Last update: 2025-03-23 06:58:52 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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 modified
  • BulkDeleteTrait: 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.