Setting value datetime-local on laravel
Solution 1
Add below accessors to your model. I think Y-m-d\TH:i
is the only date format that datetime-local
input accepts.
public function getDateStartAttribute($value)
{
return Carbon::parse($value)->format('Y-m-d\TH:i');
}
public function getDateEndAttribute($value)
{
return Carbon::parse($value)->format('Y-m-d\TH:i');
}
And in your form
<input type="datetime-local" name="date_start" value="{{$yourPassedVariable->date_start}}">
<input type="datetime-local" name="date_end" value="{{$yourPassedVariableToView->date_end}}">
And if you want to display your date fields in another format than Y-m-d\TH:i
just add another accessor to your model and use it in your views.
Solution 2
If you just want to do it in your form you can do this
<input type="datetime-local" name="date_start" value="{{ date('Y-m-d\TH:i', strtotime($yourPassedVariableToView)) }}">
<input type="datetime-local" name="date_end" value="{{ date('Y-m-d\TH:i', strtotime($yourPassedVariableToView)) }}">
Solution 3
<input id="time" name="time" type="datetime-local" value="{{old('time')?? date('Y-m-d\TH:i', strtotime($learning->time)) }}" class=" form-control @error('time') is-invalid @enderror" autocomplete="off">
Related videos on Youtube
APSB
Updated on June 04, 2022Comments
-
APSB almost 2 years
I want to add value in my edit form, with the value take from my database. but I don't know how to add value in the form of type datetime-local. I've tried but not appear.
here my view :
<div class='form-group col-sm-6'> <label>Date Start</label> <input class='form-control' value='{{ @$row->date_start }}' readonly /> <input type='datetime-local' class='form-control' name='date_start' value='{{ @$row->date_start }}' required/> </div> <div class='form-group col-sm-6'> <label>Date End</label> <input class='form-control' value='{{ @$row->date_end }}' readonly /> <input type='datetime-local' class='form-control' name='date_end' value='{{ @$row->date_end }}' required/> </div>
and here my controller :
public function postEditSave($id) { $simpan= array(); $simpan['date_start']=Request::input('date_start'); $simpan['date_end']=Request::input('date_end'); $simpan['condition_status']=Request::input('condition_status'); $simpan['id_cms_users']=Request::input('id_cms_users'); $simpan['id_cms_companies']=Request::input('id_cms_companies'); DB::table('log_patrols')->where('id', $id)->update($simpan); Session::flash('edit', 'Berhasil merubah data'); return redirect('patrols'); }
and this my database :
and for notice my date_start and date_end column is type datespam
can someone give me solution ? or did any other form input type can i use for my project ? which could take a date and time data directly? for to change type datetime-local ?
thanks...
sorry for my bad english.
-
Anees Saban over 2 yearsThe future is now... Carbon now has the "toDateTimeLocalString" format just for this.
-
-
Mark Rotteveel over 3 yearsPlease don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
-
Admin almost 2 yearsAs it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.