PHP code example of t-labs-co / booleanize

1. Go to this page and download the library: Download t-labs-co/booleanize library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

t-labs-co / booleanize example snippets


return [
// Override your configuration 
];

// use helper class 
booleanize('true', null, true); // OUTPUT: true

// or call instance and using
booleanize()->convert('Yes', null, true); // OUTPUT: true 
booleanize()->convert('Yes', null, 1); // OUTPUT: 1 

booleanize()->convert('No', null, true); // OUTPUT: false 
booleanize()->convert('No', null, 1); // OUTPUT: 0 

// model class 

class User extends Authenticatable
{
    use HasBooleanizeTrait;
    
    // cast using 
    protected function casts(): array
    {
        return [
            'agreed' => BooleanizeCast::class
        ];
    }
    
    // setting cast type SET and GET
    public function getTrueValueAs(): array
    {
        // config your default true value for your attribute when GET
        return [
            'agreed' => 1
        ];
    }
    
    public function setTrueValueAs(): array
    {
        // config your default true value for your attribute when SET
        return [
            'agreed' => 'yes'
        ];
    }
}

// query Scope using  whereBooleanize()
$users = User::whereBooleanize('agreed', true)->get(); 
// this will auto convert `true` to `yes` value from `setTrueValueAs()` and do query

// Or shorter 
$users = User::whereBooleanizeTrue('agreed')->get();


$data = [
    'term_confirm' => 'Y'
];
// term_confirm could be any in [y,yes,1,true]
$rules = [
    'term_confirm' => ['
bash
php artisan vendor:publish --tag="booleanize-config"