沃梦达 / 编程问答 / php问题 / 正文

如何更改 created_at 和 updated_at 值 laravel 的默认格式

How to change default format at created_at and updated_at value laravel(如何更改 created_at 和 updated_at 值 laravel 的默认格式)

本文介绍了如何更改 created_at 和 updated_at 值 laravel 的默认格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Laravel 的新手.我正在使用 Laravel 创建一个应用程序.当我创建帖子时,created_at"和updated_at"的值如下所示:

I am new in Laravel. I am creating a application with laravel. When i creating a post then the values of "created_at" and 'updated_at" are look like this:

2014-06-26 04:07:31
2014-06-26 04:07:31

但我希望没有时间的这些值看起来像这样:

But i want this values without time look like this :

2014-06-26
2014-06-26

只有日期没有时间.

有可能吗???

请帮帮我.

我的帖子模型:

<?php

class Post extends Eloquent {

    protected $fillable = array('title', 'body', 'meta','reporter', 'slug', 'image','top');

    public static $rules = array(
        'title'=>'required|min:2',
        'body'=>'required|min:20',
        'reporter'=> 'required|min:2',
        'image'=>'image|mimes:jpeg,jpg,bmp,png,gif'
    );

    public function categories()
    {
        return $this->belongsToMany('Category');
    }

}

我的邮政控制器商店:

public function store()
{
    $validator = Validator::make(Input::all(), Post::$rules);

    if ($validator->passes()) {
        $post = new Post;
        $post->title = Input::get('title');
        $post->body = Input::get('body');
        $post->reporter = Input::get('reporter');
        $post->meta = Input::get('meta');
        $post->slug = Input::get('title');
        $post->top = Input::get('top');

        $image = Input::file('image');
        if ($image) {
            $filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
            Image::make($image->getRealPath())->resize(250, 145)->save('public/images/postimages/'.$filename);
            $post->image = 'images/postimages/'.$filename;
        }


        $categories = Input::get('categories');

        $post->save();

        $post->categories()->sync($categories);

        return Redirect::route('admin.posts.index')
            ->with('message', 'Product Created');
    }

    return Redirect::back()
        ->with('message', 'Something went wrong')
        ->withErrors($validator)
        ->withInput();
}

请帮帮我.

推荐答案

在你的 Post 模型中添加两​​个这样的访问器方法:

In your Post model add two accessor methods like this:

public function getCreatedAtAttribute($date)
{
    return CarbonCarbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
}

public function getUpdatedAtAttribute($date)
{
    return CarbonCarbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
}

现在,每次您使用模型中的这些属性来显示日期时,这些都会以不同的方式呈现,只是不带时间的日期,例如:

Now every time you use these properties from your model to show a date these will be presented differently, just the date without the time, for example:

$post = Post::find(1);
echo $post->created_at; // only Y-m-d formatted date will be displayed

因此您无需更改数据库中的原始类型.要更改数据库中的 type,您需要将其从 Timestamp 更改为 Date 并且您需要从迁移中进行(如果您使用完全)或直接进入您的数据库,如果您不使用迁移.timestamps() 方法添加这些字段(使用 Migration),要在迁移期间更改这些字段,您需要删除 timestamps() 方法并使用 date() 代替,例如:

So you don't need to change the original type in the database. to change the type in your database you need to change it to Date from Timestamp and you need to do it from your migration (If your using at all) or directly into your database if you are not using migration. The timestamps() method adds these fields (using Migration) and to change these fields during the migration you need to remove the timestamps() method and use date() instead, for example:

$table->date('created_at');
$table->date('updated_at');

这篇关于如何更改 created_at 和 updated_at 值 laravel 的默认格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何更改 created_at 和 updated_at 值 laravel 的默认格式

基础教程推荐