PHP code example of igortrinidad / laravel-model-utilities
1. Go to this page and download the library: Download igortrinidad/laravel-model-utilities 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/ */
igortrinidad / laravel-model-utilities example snippets
namespace App;
use Illuminate\Database\Eloquent\Model;
use IgorTrinidad\ModelUtilities\Traits\UuidPrimary;
use IgorTrinidad\ModelUtilities\Traits\TitleCase;
use IgorTrinidad\ModelUtilities\Traits\FullName;
use IgorTrinidad\ModelUtilities\Traits\SanitizeEmail;
use IgorTrinidad\ModelUtilities\Traits\FormatDate;
use IgorTrinidad\ModelUtilities\Traits\FormatCurrency;
class Actor extends Model
{
use UuidPrimary, TitleCase, FullName, SanitizeEmail, FormatDate, FormatCurrency;
/**
* Here you can change the primary key of your model to the correspondent primaryKey of your table
*/
protected $primaryKey = 'id';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'last_name',
'email',
'bday',
'payroll'
];
/**
* The columns that should be applied Title Case
* This trait will format the columns that should be Title Case before saving in db
*
* @var array
*/
protected $titleCases = [
'name',
];
/**
* The attribute of your model to create the fullName firstName . ' ' . lastName * applied title case
* Set the onlyGetter attribute if you dont have a column in your DB of the fullName attribute
* If the onlyGetter is set to true we just create the full_name attribute on model retrieved from db
*
* @var array
*/
protected $fullNames = [
//This key should be the same as attribute or column name
'full_name' => [
'onlyGetter' => false,
'firstName' => 'name',
'lastName' => 'last_name'
]
];
/**
* The columns to apply email sanitization
*
* @var array
*/
protected $emailColumns = [
'email'
];
/**
* The date columns that you want to format for the end user
* The unformatted is the same of your database date format
* The formatted is the way you want to show for your end user
* This will format the date column just after retrieved of the db and unformat before saving in db
*
* @var array
*/
protected $dateColumns = [
'bday' => [
'unformatted' => 'Y-m-d',
'formatted' => 'd/m/Y'
]
];
/**
* The currency columns you want to format
* This will create a model attribute like 'formatted_value'
* 'value' is the name of the column in your dabase
*
* @var array
*/
protected $currencyColumns = [
'payroll' => [
'attr_prefix' => 'formatted_',
'prefix' => 'US$ ',
'decimal' => ',',
'thousand' => '.',
'precision' => 2
]
];
//...
}
use IgorTrinidad\ModelUtilities\Traits\UpperCaseFirst;
use IgorTrinidad\ModelUtilities\Traits\FormatCurrency;
class Product extends Model {
use UpperCaseFirst, FormatCurrency;
/**
* The columns that should be applied Upper Case for the first char
* This trait will format the columns that should be Upper before saving in db
*
* @var array
*/
protected $upperCaseFirst = [
'name',
];
/**
* The currency columns you want to format
* This will create a model attribute like 'formatted_value'
* 'value' is the name of the column in your dabase
*
* @var array
*/
protected $currencyColumns = [
'value' => [
'attr_prefix' => 'formatted_',
'prefix' => 'US$ ',
'decimal' => ',',
'thousand' => '.',
'precision' => 2
],
'discount' => [
'attr_prefix' => 'formatted_',
'prefix' => 'US$ ',
'decimal' => ',',
'thousand' => '.',
'precision' => 2
]
];
/**
*
* Or use with global config settings config/model-utilities.php
* @var array
*/
protected $currencyColumns = [
'value', 'discount
];
}
use IgorTrinidad\ModelUtilities\Traits\FormatDate;
class User extends Model {
use FormatDate;
/**
* The currency columns you want to format
* This will create a model attribute like 'formatted_value'
* 'value' is the name of the column in your dabase
*
* @var array
*/
protected $dateColumns = [
'bday' => [
'unformatted' => 'Y-m-d', //Default unformat date format
'formatted' => 'd/m/Y' // Default formatted date format
]
];
/**
*
* Or use with global config settings config/model-utilities.php
* @var array
*/
protected $dateColumns = [
'bday'
];
}
use IgorTrinidad\ModelUtilities\Traits\Uuid;
class User extends Model {
//Just add the trait inside your model
use Uuid;
//The rest of your model
}
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->uuid('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}