PHP code example of cviebrock / eloquent-typecast

1. Go to this page and download the library: Download cviebrock/eloquent-typecast 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/ */

    

cviebrock / eloquent-typecast example snippets


use Cviebrock\EloquentTypecast\EloquentTypecastTrait;

class MyModel {

    use EloquentTypecastTrait;

    // Define the attributes you want typecast here
    protected $cast = array(
        'id'         => 'integer',
        'price'      => 'float',
        'is_awesome' => 'boolean'
    );

    ...

}

class MyModel {

    use EloquentTypecastTrait;

    protected $castOnSet = true;

    protected $cast = array(
        'price'      => 'float',
    );

}

$myModel = MyModel::find(1);

$price = Input::get('price');  // this will be a string
$myModel->price = $price;      // the string is cast to a float before setting;