jeudi 29 août 2019

laravel 5.7 how to update datetime field to null

■ Laravel : 5.7

■ PHP : 7.2

I need to update model DateTime Field to null, but I don't know why when I input to null will update Field to current date time.

When I set form job_start or job_end on blank, than will update Field to current date time.

Also, I already have try $job->job_start = null; $job->save(); but get same result.

/?php

class Job extends Model {
/**
 * @var $casts(toArray、toJson)
 */
protected $casts = [
    'visit_date' => 'date:Y-m-d',
    'job_start' => 'datetime:Y-m-d H:i',
    'job_end' => 'datetime:Y-m-d H:i',
];

/**
 * @var date 
 */
protected $dates = [
    'visit_date', 
    'job_start',
    'job_end',
];

// $fillable(save、update、fill)
protected $fillable = [
    /**SKIP**/
    'visit_date', 
    'job_start',
    'job_end',
    /**SKIP**/
];

/**SKIP other function**/

/**
 * job_start
 * @param  string  $value ($value format 'Y:m:d H:i')
 * @return void
 */
public function setJobStartAttribute($value) {
    if($value !== null){
        //$value format 'Y:m:d H:i' to 'Y-m-d H:i:s'
        $value = (new Carbon($value))->format('Y-m-d H:i:s');
    }
    //set
    $this->attributes['job_start'] = $value;
}

/**
 * job_start
 * @param  string  $value ($value format 'Y:m:d H:i:s')
 * @return void
 */
public function getJobStartAttribute($value) {
        //$value format 'Y:m:d H:i:s' to 'Y-m-d H:i'
    return (new Carbon($value))->format('Y-m-d H:i');
}


}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire