Search by

t-labs-co / booleanize

t-labs-co

Config-driven boolean conversion utility for Laravel — convert yes/no, on/off, enabled/disabled, and custom truthy/falsy values to booleans, with validation rules, Eloquent casts, model query scopes, and a helper function.

Package info

github.com/T-Labs-Co/booleanize

Homepage

pkg:composer/t-labs-co/booleanize

Fund package maintenance!

ty-huynh

Statistics

Installs: 4

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 4

v2.0.0 2026-10-27 05:34 UTC

This package is auto-updated.

Last update: 2026-10-27 05:39:17 UTC


README

Tests Static Analysis PHPStan Level 8

Tools for easier boolean manipulation in Laravel.

Requirements

  • PHP ^8.4
  • Laravel ^12|^13

Installation

composer require t-labs-co/booleanize

Upgrading from v1.x? Read the Migration Guide for a step-by-step upgrade path.

Usage

Basic Conversion

use TLabsCo\Booleanize\Facades\Booleanize;

// Convert various values to boolean
Booleanize::convert('yes');   // true
Booleanize::convert('no');    // false
Booleanize::convert('on');    // true
Booleanize::convert('off');   // false
Booleanize::convert('1');     // true
Booleanize::convert('0');     // false

Helper Function

booleanize('yes');   // true
booleanize('no');    // false

// Convert arrays
booleanize(['status' => 'yes', 'active' => 'no']);
// ['status' => true, 'active' => false]

Truthy Values

The following values are recognized as true:

Value Type
y string
yes string
active string
enable string
on string
1 string
true string
good string
ok string
1 integer
true boolean

Falsy Values

The following values are recognized as false:

Value Type
n string
no string
inactive string
disable string
off string
0 string
false string
bad string
not_ok string
0 integer
false boolean

Validation Rules

use TLabsCo\Booleanize\Rules\BooleanizeRule;
use TLabsCo\Booleanize\Rules\BooleanizeTrueRule;
use TLabsCo\Booleanize\Rules\BooleanizeFalseRule;

$request->validate([
    'status' => ['required', new BooleanizeRule],
    'active' => ['required', new BooleanizeTrueRule],
    'deleted' => ['required', new BooleanizeFalseRule],
]);

Eloquent Casts

use TLabsCo\Booleanize\Casts\BooleanizeCast;
use TLabsCo\Booleanize\Casts\BooleanizeInboundCast;

class User extends Model
{
    protected $casts = [
        'is_active' => BooleanizeCast::class,
        'status' => BooleanizeInboundCast::class,
    ];
}

Model Trait

use TLabsCo\Booleanize\Model\HasBooleanizeTrait;

class User extends Model
{
    use HasBooleanizeTrait;

    public function getTrueValueAs(): array
    {
        return [
            'is_active' => 'enabled',
        ];
    }

    public function setTrueValueAs(): array
    {
        return [
            'is_active' => 'enabled',
        ];
    }
}

// Query scopes
User::whereBooleanizeTrue('is_active')->get();
User::whereBooleanizeFalse('is_active')->get();
User::whereBooleanize('is_active', 'enabled')->get();

Custom Values

Booleanize::convert('yes', null, 'enabled');  // 'enabled'
Booleanize::convert('no', null, 'enabled');   // 'disabled'

Inverse Conversion

Booleanize::inverse('yes');  // false
Booleanize::inverse('no');   // true

Human Readable

Booleanize::human('yes');  // 'Yes, I agree'
Booleanize::human('no');   // 'No, I don't'

Configuration

Publish the config file:

php artisan vendor:publish --tag=booleanize-config

Quality

composer test      # Run Pest tests
composer analyse   # Run PHPStan
composer format    # Run Laravel Pint

Credits

License

The MIT License (MIT). Please see License File for more information.