Search code examples
phpmysqllaravellaravel-4php-carbon

How to store Carbon objects in MySQL using Laravel


So, I know that Laravel stores their "created_at" and their "updated_at" as a Carbon object. At least, every time, I try to get them, the are returned as Carbon. So I have a Carbon object that I want to store in my "premium_until" column. Now, how should I go about doing this? Should I do $table->timestamp('premium_until'); or $table->date('premium_until'); when setting up the schema? Also, can I just do $user->premium_until = $carbonObject; or do I have to convert it to a string? Thanks in advance!


Solution

  • Eloquent offers convenient way to work with dates.
    Just define getDates method on your Eloquent model, which returns array of column names which should be converted into Carbon instances automaticaly by Eloquent!

    public function getDates()
    {
        return array(static::CREATED_AT, static::UPDATED_AT, static::DELETED_AT, 'premium_until');
    }