tusimo/eloquent-default is a Laravel package for set default value for eloquent.
It currently has 1 GitHub stars and 12 downloads on Packagist (latest version v0.1).
Install it with composer require tusimo/eloquent-default.
Discover more Laravel packages by tusimo
or browse all Laravel packages to compare alternatives.
Last updated
set the default value for Laravel's ORM. And define the column can save to database.
Either PHP 5.6+ is required.
To get the latest version of EloquentDefault, simply require the project using Composer:
$ composer require tusimo/eloquent-default
Instead, you may of course manually update your require block and run composer update if you so choose:
{
"require": {
"tusimo/eloquent-default": "^0.1"
}
}
Within your eloquent model class add following line
use Tusimo\EloquentDefault\DefaultTrait;
class User extends Model {
use DefaultTrait;
/**
* define the columns the table has
* and define the default value saving to the database when attributes not set the column
*/
protected $defaults = [
'_id',
'gender' => 1,
'status' => 0,
'deleted_at' => null,
];
/**
* when save attributes to database ,columns only defined in the defaults will save to database
* default is false and we do nothing when the column is not in the database and this will throw
* a exception when you using mysql or the data will saving to the database when you using mongo
*/
protected $columnStrict = true;
/**
*Also you can define a method as `get*Default` to set a default value
*/
preotected function getCustomDefault($custom)
{
return \Auth::isAdmin ? 'admin' : 'user';
}
}
The MIT License (MIT). Please see License File for more information.