<?php

namespace App\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class BaseModel extends Model
{
    use HasFactory;

    /**
     * @param DateTimeInterface $date
     * @return string
     * 重写模型的serializeDate方法
     * "created_at": "2022-05-11T14:31:02.000000Z", 格式变为 "created_at": "2022-05-11 14:45:25",
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    protected function getCreatedAtAttribute($value)
    {
        return date('Y-m-d H:i', strtotime($value));
    }

    protected function getUpdatedAtAttribute($value)
    {
        return date('Y-m-d H:i', strtotime($value));
    }

    protected function getDeletedAtAttribute($value)
    {
        return date('Y-m-d H:i', strtotime($value));
    }

    //去掉 created_at， updated_at, deleted_at
    public static function formatData($data)
    {
        if(isset($data['created_at'])){
            unset($data['created_at']);
        }
        if(isset($data['updated_at'])){
            unset($data['updated_at']);
        }
        if(isset($data['deleted_at'])){
            unset($data['deleted_at']);
        }
        return $data;
    }

    public function scopeNotDelete($query)
    {
        return $query->whereNull('deleted_at');
    }


}
