Laravel dd / mm / yyyy to yyyy / mm / dd / conversion when using model for db operations

Here is my controller

public function DriverProcess()
    {            
     $DriverData = Input::all();
     $validation = Validator::make($DriverData, DriverModel::$rules);
     if ($validation->passes())
        {
            DriverModel::create($DriverData);
     //Rest of Code 

      

Here is my model

<?php
class DriverModel extends Eloquent 
{


    protected $guarded = array('DriverName');
    protected $fillable = array('DriverName', 'Age', 'Address', 'LicenseNumber', 'DateOfBirth', 'LicenseExpiry' );

      

In the field DateOfBirth

andLicenseExpiry

I am sending date values ​​as dd/mm/yyyy

Format

As I post using Model DriverModel::create($DriverData);

and using fillable

I don't know where to convert dd/mm/yyyy

toyyyy/mm/dd

Note:

I know to convert date format to date("Y-m-d", strtotime($var) );

But I need to know where I can perform this operation.

+3


source to share


1 answer


You can use mutators for this in your model (see http://laravel.com/docs/4.2/eloquent#accessors-and-mutators ). In your case, it would be:

public function setDateOfBirthAttribute($value)
{
    $this->attributes['DateOfBirth'] = date("Y-m-d", strtotime($value) );
}

      



You should use only $fillable

or $guarded

in your model as a support . Right now, you have the same attribute as in $guarded

and $fillable

.

+3


source







All Articles