Search code examples
laraveltempus-dominus-datetimepicker

How to display stored value of datetimepicker to blade in Laravel


Do you know guys how display the date value from database to datetimepicker field?

the example value from database is 2022-10-31

<div class="input-group date" id="allDay" data-target-input="nearest">
    <input type="text" id="all-value" name="all_day"
        class="form-control datetimepicker-input" data-target="#allDay"
        value="" />
    <div class="input-group-append" data-target="#allDay" data-toggle="datetimepicker">
         <div class="input-group-text"><i class="fa fa-calendar"></i></div>
    </div>
</div>

js

$('#allDay,#startLongDay,#endLongDay').datetimepicker({
    format: 'L',
    minDate: new Date()
});

Solution

  • If you're using the jQuery DateTime Picker, You can retrieve the value from the database and then pass it to the view through your controller. After that, you can set it as the value attribute.

    Example:

    <input type="text" id="all-value" name="all_day"
            class="form-control datetimepicker-input" data-target="#allDay"
            value="{{$date}}" />
    

    Or else you can do it in JS Way:

    1. Setting with viewDate option
    <script>
    var d = "{{$date}}"
    $('#allDay,#startLongDay,#endLongDay').datetimepicker('viewDate', d);
    </script>
    
    1. Setting with date option
    <script>
    var d = "{{$date}}"
    $('#allDay,#startLongDay,#endLongDay').datetimepicker('date', d);
    </script>
    

    Please note that in all of the above steps you need to format the date correctly.