user3369524
user3369524

Reputation: 41

Laravel 4 save dates on database

I want to save dates using laravel 4, I have been looking on the internet but the only good solution that I found was using

$start_at = date("Y-m-d", strtotime($start_at));

but I am sure that this is kind of archaic code used by Isaac Newton xd, plus if I want to retrieve it, I need to used again. In the manual, I found the "variable protected $dates = array('My_date', 'Another_date');" or using the "public function getDates ()", http://laravel.com/docs/eloquent#date-mutators I even change the file Model.php, but I have this error all the time "Unexpected data found. Unexpected data found. Data missing"

class Insurance extends Eloquent {
    protected $table = 'insurance';
    protected $dates = array('start_at', 'end_at');
    public $timestamps = false;
    protected $primaryKey = 'id_insurance';
    protected $fillable = array (
            'id_vehicle',
            'start_at',
            'end_at' 
    );

    public function getDates()
    {
        return array('start_at', 'end_at');
    }
}

Upvotes: 0

Views: 270

Answers (1)

Himanshu Sharma
Himanshu Sharma

Reputation: 456

I would suggest using the Carbon library for PHP.

Set your column type in the database as date. (mySql allows that).

If your form has inputs which give separate parts of the Date such as year, month and Date, you could create the input to be inserted as

Carbon::createFromDate(      
 $dateOfBirth['year'],      
 $dateOfBirth['month'],
 $dateOfBirth['date'])->toDateString();

This can be inserted into your eloquent Model.

Upvotes: 1

Related Questions