PHPackages                             laracraft-tech/laravel-useful-traits - 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. laracraft-tech/laravel-useful-traits

Abandoned → [laracraft-tech/laravel-useful-additions](/?search=laracraft-tech%2Flaravel-useful-additions)Library[Utility &amp; Helpers](/categories/utility)

laracraft-tech/laravel-useful-traits
====================================

A collection of useful Laravel additions!

v4.2.0(1mo ago)582.7k↓33.3%2[1 issues](https://github.com/laracraft-tech/laravel-useful-additions/issues)MITPHPPHP ^8.0CI passing

Since Feb 1Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/laracraft-tech/laravel-useful-additions)[ Packagist](https://packagist.org/packages/laracraft-tech/laravel-useful-traits)[ Docs](https://github.com/laracraft-tech/laravel-useful-additions)[ RSS](/packages/laracraft-tech-laravel-useful-traits/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (22)Versions (17)Used By (0)

A collection of useful Laravel additions!
=========================================

[](#a-collection-of-useful-laravel-additions)

[![Latest Version on Packagist](https://camo.githubusercontent.com/c7711c7ffc4913a6caaadc245e9df74a16d8dcd2b6aefc9997c21a495816df36/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c61726163726166742d746563682f6c61726176656c2d75736566756c2d6164646974696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/laracraft-tech/laravel-useful-traits)[![Tests](https://github.com/laracraft-tech/laravel-useful-additions/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/laracraft-tech/laravel-useful-traits/actions/workflows/run-tests.yml)[![Check & fix styling](https://github.com/laracraft-tech/laravel-useful-additions/actions/workflows/fix-php-code-style-issues.yml/badge.svg?branch=main)](https://github.com/laracraft-tech/laravel-useful-traits/actions/workflows/fix-php-code-style-issues.yml)[![License](https://camo.githubusercontent.com/d6d822d55ed4f377de9ade253f6c3046e9de105a1ce15e9ff9916d8768f401f6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c61726163726166742d746563682f6c61726176656c2d75736566756c2d6164646974696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/laracraft-tech/laravel-useful-traits)[![Total Downloads](https://camo.githubusercontent.com/29d2d95cf52d84b4761ae2e169d0cecc50d63af195206d74d6953356764c9eca/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c61726163726166742d746563682f6c61726176656c2d75736566756c2d6164646974696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/laracraft-tech/laravel-useful-traits)

Here we will share some useful Laravel additions we need in our daily work.

### Traits

[](#traits)

- [`UsefulEnums`](#usefulenums)
- [`UsefulScopes`](#usefulscopes)
    - [`selectAllBut`](#selectallbut)
    - [`fromToday`](#fromtoday-fromyesterday)
    - [`fromYesterday`](#fromtoday-fromyesterday)
- [`RefreshDatabaseFast`](#refreshdatabasefast) *(deprecated)*

### Commands

[](#commands)

- [`db:truncate`](#dbtruncate)

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

[](#installation)

You can install the package via composer:

```
composer require laracraft-tech/laravel-useful-additions
```

Then publish the config file with:

```
php artisan vendor:publish --tag="useful-additions-config"
```

Traits
------

[](#traits-1)

### UsefulEnums

[](#usefulenums)

This trait is only available with **PHP 8.1** or higher installed.

---

#### `names`, `values`, `array`

[](#names-values-array)

This could be very handy if you like to **loop** over all of your **enum** types, or you maybe want to use the enum as an array, for instance in a migration.

```
use LaracraftTech\LaravelUsefulAdditions\Traits\UsefulEnums;

enum PaymentType: int
{
    use UsefulEnums;

    case Pending = 1;
    case Failed = 2;
    case Success = 3;
}

PaymentType::names();   // return ['Pending', 'Failed', 'Success']
PaymentType::values();  // return [1, 2, 3]
PaymentType::array();   // return ['Pending' => 1, 'Failed' => 2, 'Success' => 3]
```

### UsefulScopes

[](#usefulscopes)

---

#### `selectAllBut`

[](#selectallbut)

Select all columns but given excluded array.

```
use LaracraftTech\LaravelUsefulAdditions\Traits\UsefulScopes;

$class = new class extends Model
{
    use UsefulScopes;

    protected $timestamps = false;
    protected $table = 'scope_tests';
};

$class->create([
    'foo' => 'foo',
    'bar' => 'bar',
    'quz' => 'quz',
]);

$class::query()->selectAllBut(['foo'])->first()->toArray();
// return ['bar' => 'bar', 'quz' => 'quz']
```

***Note***: Since you **can't** do a native "**select all but** x,y,z" in mysql, we need to query (and cache) the existing columns of the table, and then exclude the given columns which should be **ignored** (not selected) from the existing columns.

***Cache***: Column names of each table will be cached **until** contents of migrations **directory** is added or deleted. **Modifying** the contents of files inside the migrations directory will not re-cache the columns. Consider to **clear the cache** whenever you make a new **deployment/migration**!

---

#### `fromToday`, `fromYesterday`

[](#fromtoday-fromyesterday)

Select all entries created today or yesterday.

```
use LaracraftTech\LaravelUsefulAdditions\Traits\UsefulScopes;

$class = new class extends Model
{
    use UsefulScopes;

    protected $timestamps = true;
    protected $table = 'scope_tests';
};

$class->create(['foo' => 'foo1', 'bar' => 'bar1', 'quz' => 'quz1']);
$class->create(['foo' => 'foo2', 'bar' => 'bar2', 'quz' => 'quz2', 'created_at' => now()->yesterday()]);

$class::select('foo')->fromToday()->first()->toArray(); // return ['foo' => 'foo1']
$class::select('foo')->fromYesterday()->first()->toArray(); // return ['foo' => 'foo2']
```

### RefreshDatabaseFast

[](#refreshdatabasefast)

> **Deprecated:** We recommend using Laravel's built-in `RefreshDatabase` trait instead.
>
> Laravel now uses a similar approach internally (migrate once, then rollback per test), which makes this trait largely redundant. Additionally, `RefreshDatabaseFast` can cause **persistent data across tests** when a test is aborted unexpectedly, because the rollback may not be triggered. The small performance gain of skipping the initial migration via checksum is minimal compared to the potential issues this can cause.
>
> Use `Illuminate\Foundation\Testing\RefreshDatabase` for a more resilient testing experience.

---

This is a trait which makes the migration of your database in your test suite much, **much faster**! The base idea comes from [Mayahi](https://mayahi.net/laravel/make-refresh-database-trait-much-faster/). It basically **only** migrates your database if the **migration** files has **changed**. So the first `migrate:fresh` takes a while (depending on how many migrations you have), and then it's incredible fast.

Optionally you can set `USEFUL_ADDITIONS_SEED_AFTER_FAST_DB_REFRESH` to `true` if you like to seed your database after the migration.

Also make sure to add the `.phpunit.database.checksum` to your `.gitignore` file!

***Pest:***

```
use LaracraftTech\LaravelUsefulAdditions\Traits\RefreshDatabaseFast;

uses(RefreshDatabaseFast::class);

it('does_something', function() {
    // ...
});
```

***PHPUnit:***

```
use LaracraftTech\LaravelUsefulAdditions\Traits\RefreshDatabaseFast;
use Tests\TestCase;

class MyTest extends TestCase
{
    use RefreshDatabaseFast;

    /** @test **/
    public function it_does_something()
    {
        // ...
    }
}
```

Commands
--------

[](#commands-1)

### db:truncate

[](#dbtruncate)

---

This command truncates all the tables of your current database connection. Checkout `--help` to see arguments and options. It for instance, lets you also truncate only **specific** tables or disable **foreigen key checks** or maybe run in **force** mode.

```
php artisan db:truncate
```

```
INFO  Start truncating tables.

Truncating table: failed_jobs .............................................. 135ms DONE
Truncating table: migrations ................................................ 87ms DONE
Truncating table: password_reset_tokens ..................................... 79ms DONE
Truncating table: personal_access_tokens .................................... 86ms DONE
Truncating table: users ..................................................... 78ms DONE

INFO  Finished truncating tables.

```

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)

- [Zacharias Creutznacher](https://github.com/laracraft-tech)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance89

Actively maintained with recent releases

Popularity32

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 73.1% 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 ~88 days

Recently: every ~271 days

Total

14

Last Release

49d ago

Major Versions

v1.0.3 → v2.0.02023-02-24

v2.0.0 → v3.0.02023-03-12

v3.4.0 → v4.0.02024-03-07

PHP version history (3 changes)v1.0.0PHP ^8.1

v3.4.0PHP ^7.4 || ^8.0

v4.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/cf347b32c0d1fceabbcbce7626ad662faa34738396fd9f774231d55370823713?d=identicon)[Sairahcaz](/maintainers/Sairahcaz)

---

Top Contributors

[![Sairahcaz](https://avatars.githubusercontent.com/u/7384870?v=4)](https://github.com/Sairahcaz "Sairahcaz (95 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (21 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (14 commits)")

---

Tags

enumslaravelphpscopesseedtruncatelaravellaracraft-techlaravel-useful-additions

###  Code Quality

TestsPest

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/laracraft-tech-laravel-useful-traits/health.svg)

```
[![Health](https://phpackages.com/badges/laracraft-tech-laravel-useful-traits/health.svg)](https://phpackages.com/packages/laracraft-tech-laravel-useful-traits)
```

###  Alternatives

[laracraft-tech/laravel-useful-additions

A collection of useful Laravel additions!

58109.4k](/packages/laracraft-tech-laravel-useful-additions)[spatie/laravel-health

Monitor the health of a Laravel application

85810.0M83](/packages/spatie-laravel-health)[spatie/laravel-enum

Laravel Enum support

3655.4M31](/packages/spatie-laravel-enum)[psalm/plugin-laravel

Psalm plugin for Laravel

3274.9M308](/packages/psalm-plugin-laravel)[spatie/laravel-livewire-wizard

Build wizards using Livewire

4061.0M4](/packages/spatie-laravel-livewire-wizard)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)

PHPackages © 2026

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