WNR
WNR

Reputation: 81

Laravel Livewire - how to insert model data into datetime-local input by wire

i got a HTML <input> of datetime-local with is wired to some model, when i want to save data its fine but when i want to load modal with data of existing model my input date-timelocal is not loading values.

<x-jet-input id="date" class="block mt-1" type="datetime-local" name="date" wire:model='date'/>

I tried:

protected $casts = [
    'date' => 'datetime:Y-m-d\TH:i:s'
];

But it doesnt change anythin, my <input> is not not loading data. I think its becouse 'date' returns wrong format anyway. Like "2021-10-07 00:00:00".

Upvotes: 0

Views: 3482

Answers (2)

Ali Haider
Ali Haider

Reputation: 41

You can use laravel accessor in laravel 8 you can just simply do this in your model

public function getDateAttribute($value)
{
    return \Carbon\Carbon::create($value)->format("Y-m-d\TH:i:s");
}

Further you can read more about accessors and mutators from the official document Accessors & Mutators

Upvotes: 1

IGP
IGP

Reputation: 15786

$casts only affects the value when it's casted to an array or json format. You'll have to format the value in the livewire component with Carbon's formatmethod.

$date = $date->format('Y-m-d\TH:i:s');

Upvotes: 1

Related Questions