Laravel Booleanize

Boolean value conversion and casting for Laravel

2.0.0 GitHub ActionsMITPHP ^8.4Laravel ^12.0 | ^13.0

Convert strings, integers, and mixed values to boolean with ease. Cast Eloquent model attributes, query boolean fields, validate input, and use helper functions — all with a clean, configurable API.

Requirements

Core Package
  • PHP ^8.4
  • Laravel ^12.0 | ^13.0

Install with Composer

composer require t-labs-co/booleanize

Get started in minutes

1. Install the package

composer require t-labs-co/booleanize

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

2. Convert values to boolean

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

3. Use the helper function

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

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

4. 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],
]);

5. Cast Eloquent model attributes

Use BooleanizeCast and BooleanizeInboundCast to automatically cast attributes:

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

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

6. Model trait with query scopes

use TLabsCo\Booleanize\Model\HasBooleanizeTrait;

class User extends Model
{
    use HasBooleanizeTrait;
}

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

Features

Truthy and Falsy Values

The following values are recognized as true:

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

The following values are recognized as false:

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

Value conversion

Convert between different boolean representations:

  • convert() — Returns PHP true/false
  • inverse() — Returns the opposite boolean
  • human() — Returns human-readable text (e.g., “Yes, I agree”)
  • Custom conversion with configurable truthy/falsy mappings

Eloquent casting

  • BooleanizeCast — Converts on both get and set
  • BooleanizeInboundCast — Converts only on set (inbound)

Model trait

The HasBooleanizeTrait provides query scopes for filtering by booleanized fields, with support for custom true/false value mappings.


Configuration

Publish the configuration file to customize truthy/falsy value mappings:

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

For complete documentation, see the README on GitHub.

Hire me for Laravel data transformation and utility work

Need a package tailored to your application? From recurring Laravel maintenance tasks to reusable components, I can help design, build, and review production-ready solutions.

Discuss your project