Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
704 views
in Technique[技术] by (71.8m points)

php - How to change default format at created_at and updated_at value laravel

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

Only date without time.

Is it possible ???

Please help me.

My Post Model:

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

}

My Post Controller store:

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();
}

Please Help me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...