PHPackages                             t-labs-co/trait-and-helper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. t-labs-co/trait-and-helper

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

t-labs-co/trait-and-helper
==========================

Collection of traits and helpers for Laravel.

1.1.1(11mo ago)19[4 PRs](https://github.com/T-Labs-Co/trait-and-helper/pulls)MITPHPPHP ^8.4||^8.3||^8.2CI passing

Since Mar 22Pushed 1mo agoCompare

[ Source](https://github.com/T-Labs-Co/trait-and-helper)[ Packagist](https://packagist.org/packages/t-labs-co/trait-and-helper)[ Docs](https://github.com/t-labs-co/trait-and-helper)[ GitHub Sponsors](https://github.com/ty-huynh)[ RSS](/packages/t-labs-co-trait-and-helper/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (13)Versions (9)Used By (0)

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)

[![Latest Version on Packagist](https://camo.githubusercontent.com/4a4506cf0f382838c283faeac160c92a92867c8c7c53be948d26c0aed33297f7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f742d6c6162732d636f2f74726169742d616e642d68656c7065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/t-labs-co/trait-and-helper)[![GitHub Tests Action Status](https://camo.githubusercontent.com/0c4fe71665fd3828a1930979213921c6ee4ac4a60eb30d8cb3be69c315e797c0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f742d6c6162732d636f2f74726169742d616e642d68656c7065722f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/t-labs-co/trait-and-helper/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/7ed763acdedba9f3a418c1034282f6f33e16b93b12f9208974cbd52973a7f959/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f742d6c6162732d636f2f74726169742d616e642d68656c7065722f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/t-labs-co/trait-and-helper/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/c318d0f6930bc859e179d4740c5ab075cbe43be365ae92032c8dfebf791b15e5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f742d6c6162732d636f2f74726169742d616e642d68656c7065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/t-labs-co/trait-and-helper)

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
------------

[](#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
--------

[](#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.
- `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.

PHP and Laravel Version Support
-------------------------------

[](#php-and-laravel-version-support)

This package supports the following versions of PHP and Laravel:

- PHP: `^8.2`
- Laravel: `^11.0`, `^12.0`

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require t-labs-co/trait-and-helper
```

Usage
-----

[](#usage)

```
###
# 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();
```

PropertyConfigurable
====================

[](#propertyconfigurable)

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.
========================================================================================================================================================================================================================================================

[](#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');
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [T.Labs &amp; Co](https://github.com/t-labs-co)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance74

Regular maintenance activity

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 76% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~26 days

Total

4

Last Release

337d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b8096712a46f142a708061822c0644b806cd1000cb5499e408c6b11b12f6a899?d=identicon)[t-labs-co](/maintainers/t-labs-co)

---

Top Contributors

[![ty-huynh](https://avatars.githubusercontent.com/u/1597540?v=4)](https://github.com/ty-huynh "ty-huynh (19 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (3 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (3 commits)")

---

Tags

helperslaravellaravel-modelpackagephpt-labs-cotraitslaravelhelperstraitsT.Labs Co.T.Labs and Co.trait-and-helper

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/t-labs-co-trait-and-helper/health.svg)

```
[![Health](https://phpackages.com/badges/t-labs-co-trait-and-helper/health.svg)](https://phpackages.com/packages/t-labs-co-trait-and-helper)
```

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.7k28.9M627](/packages/spatie-laravel-data)[hirethunk/verbs

An event sourcing package that feels nice.

513162.9k6](/packages/hirethunk-verbs)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
