I'm trying to convert the date_of_birth
column's date format from Y-m-d
to d-m-Y
.
Below is my model:
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
class Stock extends Model
{
protected $primaryKey = 'tag_no';
public $incrementing = false;
protected $fillable = [
'tag_no',
'stock_type',
'date_of_birth',
];
protected $dates=[
'created_at',
'updated_at',
'deleted_at',
'date_of_birth'
];
public function getDateOfBirthAttribute($value)
{
return Carbon::parse($value)->format('Y-m-d');
}
public function setDateOfBirthAttribute($value)
{
$this->attributes['date_of_birth'] = date('d-m-Y', strtotime($value));
}
}
Is this the right way?