I am using Laravel accessor to access value of date field in specific format. But, I am getting error from carbon library of InvalidArgument. Below is my code.
<?php
namespace App\modules\hrm;
use App\Helper\DateFormat;
use Illuminate\Database\Eloquent\Model;
class Holiday extends Model
{
protected $guarded = [];
protected $dates = ['off_date'];
public function setOffDateAttribute($date)
{
$this->attributes['off_date'] = DateFormat::convertToDBFormat($date);
}
public function getOffDateAttribute($date){
return DateFormat::convertToUIFormat($date);
}
}
Helper File
<?php
namespace App\Helper;
use Carbon\Carbon;
class DateFormat
{
public static function convertToDBFormat($date) {
return Carbon::createFromFormat('d/m/Y', $date)->format('Y-m-d');
}
public static function convertToUIFormat($date) {
return Carbon::createFromFormat('Y-m-d', $date)->format('d/m/Y');
}
}
?>
I am getting error from Carbon but I think I am doing something wrong while sending those values, maybe i am not using the laravel accessor feature properly.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire