PHP code example of canducci / eloquent-formatted

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

    

canducci / eloquent-formatted example snippets


use Formatted, FormattedDate, FormattedCurrency, FormattedBoolean;

protected function setFormattedItems(): void 
{
    $this->addFormattedItem('birthday', ['date', ['birthday', 'Y-m-d', 'd/m/Y']]);
    $this->addFormattedItem('cost', ['currency', ['cost', 2, ',', '.']]);
    $this->addFormattedItem('active', ['boolean', ['active', 'y', 'n']]);
}

protected $appends = [
    'formatted'
];

 namespace Models;

use Canducci\Formatted\Traits\Formatted;
use Canducci\Formatted\Traits\FormattedBoolean;
use Canducci\Formatted\Traits\FormattedCurrency;
use Canducci\Formatted\Traits\FormattedDate;
use Canducci\Formatted\Traits\FormattedDatetime;
use Illuminate\Database\Eloquent\Model as Eloquent;

class People extends Eloquent
{
    use Formatted, FormattedDate, FormattedCurrency, FormattedBoolean, FormattedDatetime;

    protected $table = 'peoples';
    protected $primaryKey = 'id';

    protected $fillable = [
        'name', 
        'cost', 
        'birthday',
        'active'
    ];

    protected $dates = [
        'created_at', 
        'updated_at'
    ];

    protected $appends = [
        'formatted'
    ];

    protected function setFormattedItems(): void 
    {
        $this->addFormattedItem('birthday', ['date', ['birthday', 'Y-m-d', 'd/m/Y']]);
        $this->addFormattedItem('created_at', ['dateTime', ['created_at']]);
        $this->addFormattedItem('updated_at', ['datetime', ['updated_at']]);
        $this->addFormattedItem('cost', ['currency', ['cost', 2, ',', '.']]);
        $this->addFormattedItem('active', ['boolean', ['active', 'y', 'n']]);

    }
}

array(8) {
  ["id"]=>
  int(1)
  ["name"]=>
  string(6) "Test 1"
  ["cost"]=>
  string(4) "1000"
  ["birthday"]=>
  string(10) "1999-01-01"
  ["active"]=>
  string(1) "1"
  ["updated_at"]=>
  string(27) "2021-08-23T22:32:57.000000Z"
  ["created_at"]=>
  string(27) "2021-08-23T22:32:57.000000Z"
  ["formatted"]=>
  object(stdClass)#25 (3) {
    ["birthday"]=>
    string(10) "01/01/1999"
    ["cost"]=>
    string(8) "1.000,00"
    ["active"]=>
    string(1) "n"
  }
}