3

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.

1
  • The future is now... Carbon now has the "toDateTimeLocalString" format just for this. Commented Oct 13, 2021 at 13:05

5 Answers 5

26

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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)) }}">

Comments

4
<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">

1 Comment

Please 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.
2

it \ you can use on model very easy implement on your model.

protected $casts = [
        'inicio'  => 'datetime:Y-m-d\TH:i'
        ,'entrega'  => 'datetime:Y-m-d\TH:i'
   ];

return all data formated for use.

Comments

1
<input id="time" name="time" type="datetime-local" value="{{ now()->setTimezone('T')->format('Y-m-dTh:m') }}" form-control>

Just add this in the blade

1 Comment

As 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.