Support the ongoing development of Laravel.io →
posted 10 years ago
Database
Last updated 2 years ago.
0

3 options i can think of (assuming i understand your question correctly, as it's a little broken english).

  1. There already exists a "CREATED_AT" timestamp which you could use. You can change the name of column in your database if you like by editing your model like so:

    
    class MyCustomModel extends Model {
        const CREATED_AT = "date_started";
    }
    
    

    the created_at timestamp should only ever be set on new records, and there should be nothing stopping you manually changing that (such as in a form) afterwards.

  2. Whenever you create a new record, manually set a date_started attribute to the current date in the list of attributes you want to save. You can either do this along with the other attributes you set on the model, or you can use model events as described here: http://laravel.com/docs/eloquent#model-events

  3. You cannot use "default to current date" in MySQL for anything other than TIMESTAMP fields, if you want to stay with a human readable date or datetime field, then you cannot do this, otherwise change it to this:

    $table->timestamp('date_started')->default(DB::raw('CURRENT_TIMESTAMP'));

I recommend the first option as the best option though

Last updated 2 years ago.
0

Thank you very much! This did the trick! I'm going to use the thirth option or make those field timestamps in the future

Last updated 2 years ago.
0

leesherwood said:

[ snip ]

  1. You cannot use "default to current date" in MySQL for anything other than TIMESTAMP fields, if you want to stay with a human readable date or datetime field, then you cannot do this, [ snip ]

I assume you're saying that TIMESTAMP fields are not human-readable in MySQL? If so then I believe that's true for older versions of MySQL but since version 5.6.5, I believe they're stored the same as a datetime (as per this MySQL page).

Last updated 2 years ago.
0

In Laravel 5.1 use

$table->timestamp('date_started')->useCurrent = true;
0

Sign in to participate in this thread!

Eventy

Your banner here too?

synbits synbits Joined 17 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.