PHP code example of tusimo / eloquent-default

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

    

tusimo / eloquent-default example snippets


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';
    }
}