laravel框架中created_at、updated_at、deleted_at字段返回格式处理

追梦小窝 2019-05-27 12:47:08 159浏览 0条评论
首页/ 正文
分享到: / / / /

我们在使用laravel框架开发的时候有时,created_at、updated_at、deleted_at这三个字段是自动维护的

但是我们有时候需要定制化的返回格式,比如直接返回年月日,或者返回时间戳,或者返回多少小时以前,多少分钟以前

这时候就需要我们手动修改后返回了,但是每次在查询后修改在返回这样效率显得有些低了,于是我就尝试能不能直接

在模型中查询出需要的数据格式,直接返回,下面直接贴代码了


<?php

namespace App\Models;


use Carbon\Carbon;
use Illuminate\Database\Eloquent\SoftDeletes;
use Nette\Utils\DateTime;

class FansDayChart extends Base
{
    use SoftDeletes;
    //表名
    protected $table = 'fans_day_chart';
    //主键
    protected $primaryKey = 'id';
    //过滤黑名单字段
    public $guarded = [];


    /**
     * 格式化返回时间
     * @param $timestamp
     * @return string
     * @throws \Exception
     */
    public static function getCreatedAtAttribute($timestamp)
    {
        $date = new DateTime();
        $timestamp = $date->setTimestamp($timestamp);
        if (Carbon::now() < Carbon::parse($timestamp)->addDays(10)) {
            return Carbon::parse($timestamp)->diffForHumans();
        }
        return $timestamp->format('Y-m-d H:i:s');
    }

    /**
     * 格式化返回时间
     * @param $timestamp
     * @return string
     * @throws \Exception
     */
    public static function getUpdatedAtAttribute($timestamp)
    {
        $date = new DateTime();
        $timestamp = $date->setTimestamp($timestamp);
        if (Carbon::now() < Carbon::parse($timestamp)->addDays(10)) {
            return Carbon::parse($timestamp)->diffForHumans();
        }
        return $timestamp->format('Y-m-d H:i:s');
    }
}

相信大家在这个模型里面大致已经猜到了,没错上面模型中的两个方法就是关键


getCreatedAtAttribute()、getUpdatedAtAttribute()分别对应的是created_atupdated_at字段的
他们负责获取时间的格式,并且返回,这里的get******AtAttribute符号部分就是对应字段的名称,首字母
要大写,然后默认会传入时间戳这里在理需要的模型加入尚需方法后,框架就会使用当前申明的查询方法覆盖
框架默认的时间戳处理方法

返回的数据例子如下


{
    "code": 20000,
    "data": {
        "current_page": 1,
        "data": [
            {
                "id": 4,
                "appid": "wxc23f088c4a4f9dde",
                "name": "追梦小屋",
                "price": 0,
                "total": 0,
                "created_at": "1天前",
                "updated_at": "3小时前",
                "deleted_at": null
            },
            {
                "id": 5,
                "appid": "wx24f1bdc282e9141d",
                "name": "追梦小窝",
                "price": 0,
                "total": 0,
                "created_at": "1天前",
                "updated_at": "3小时前",
                "deleted_at": null
            },
            {
                "id": 6,
                "appid": "wx3cc*****f3b1922",
                "name": "******",
                "price": 0,
                "total": 0,
                "created_at": "1天前",
                "updated_at": "3小时前",
                "deleted_at": null
            },
            {
                "id": 1,
                "appid": "wxc23f088c4a4f9dde",
                "name": "追梦小屋",
                "price": 0,
                "total": 0,
                "created_at": "3天前",
                "updated_at": "1天前",
                "deleted_at": null
            },
            {
                "id": 2,
                "appid": "wx24f1bdc282e9141d",
                "name": "追梦小窝",
                "price": 0,
                "total": 0,
                "created_at": "3天前",
                "updated_at": "1天前",
                "deleted_at": null
            },
            {
                "id": 3,
                "appid": "wx3cc4****f3b1922",
                "name": "******",
                "price": 9,
                "total": 3,
                "created_at": "3天前",
                "updated_at": "1天前",
                "deleted_at": null
            }
        ],
        "first_page_url": "https://api.54zm.com/wechat/statistics/day_chart?page=1",
        "from": 1,
        "last_page": 1,
        "last_page_url": "https://api.54zm.com/wechat/statistics/day_chart?page=1",
        "next_page_url": null,
        "path": "https://api.54zm.com/wechat/statistics/day_chart",
        "per_page": 15,
        "prev_page_url": null,
        "to": 6,
        "total": 6
    },
    "message": "ok"
}


最后修改:2020-06-20 13:03:08 © 著作权归作者所有
如果觉得我的文章对你有用,请随意赞赏
上一篇

发表评论

说点什么吧~

评论列表

扫一扫支付