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 :
here
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.
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.
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)) }}">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With