JesseyChen / Blog

lumen/laravel+swagger,在框架上集成文档和测试工具
0 stars 0 forks source link

laravel/lumen模型维护时间戳,查询转换datetime #2

Open JesseyChen opened 5 years ago

JesseyChen commented 5 years ago
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{

    protected $dateFormat = 'U';   # 模型自动维护时间戳格式

    protected $hidden = ['password', 'pay_password', 'salt'];

    public function getCreatedAtAttribute($value)   # 查询自动转化日期
    {
        return Carbon::createFromTimestamp($value)->toDateTimeString();
    }

    public function getUpdatedAtAttribute($value)
    {
        return Carbon::createFromTimestamp($value)->toDateTimeString();
    }
}