class Order extends Model{
public function getDateTime(){
$unixtime = strtotime($this->date . (!is_null($this->time) ? ' '. $this->time : ''));
return new DateTime($unixtime);
}
protected $casts = [];
}
use Illuminate\Database\Eloquent\Casts\Attribute;
class Order extends Model{
protected $casts = ['date_time' => 'datetime:Y-m-d H:00'];
function dateTime(): Attribute
{
return Attribute::make(
get: fn () => $this->castAttribute(
'date_time',
($this->date ?: '1970-01-01') . ' ' . ($this->time ?: '10:00:01')
)
)
);
}
}
# php artisan tinker
App\Models\Order::first()->date_time;
# = Illuminate\Support\Carbon @0 {#7053
# date: 1970-01-01 00:00:00.0 UTC (+00:00),
# }