t-labs-co / booleanize
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.
Fund package maintenance!
v2.0.0
2026-10-27 05:34 UTC
Requires
- php: ^8.4
- illuminate/contracts: ^12.0|^13.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.0|^5.0
- pestphp/pest-plugin-arch: ^3.0|^4.0|^5.0
- pestphp/pest-plugin-laravel: ^4.0|^5.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-27 05:39:17 UTC
README
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.