1

I have table 'Trip', one of its fields is 'schedule'. The type of 'schedule' is datetime format. I create a project in laravel. In the Combobox, it displays '2020-04-29 10:09:00', but I want it to be displayed as '29 April 2020'. Do you have any idea how to solve it? Thank you. Here's my code.

OrderController :

    public function getTrip(){
        $departure = Input::get('departure_id');
        $destination = Input::get('destination_id');

        $trip_a = Trip::select('schedule')
            ->where(['departure_id' => $departure, 'destination_id' => $destination])
            ->whereDate('schedule', '>', Carbon::now())
            ->get();

        return response()->json($trip_a);
    }

In web.php

Route::get('/order_trip', 'OrderController@getTrip');

In create.blade.php

<div class="col-sm-6">
     <label>Departure From</label>
     <select class="form-control" name="departure_id" id="departure_id">
             <option disabled selected value> -- Departure From -- </option>
                     @foreach($town as $t)
                         <option value="{{ $t->town_id }}">
                                 {{$t->town_name}}
                         </option> 
                     @endforeach 
     </select>
</div>
     @if($errors->has('departure_id'))
         <div class="text-danger">
              {{ $errors->first('departure_id')}}
         </div>
     @endif

<div class="col-sm-6">
     <label>Destination</label>
      <select class="form-control" name="destination_id" id="destination_id">
             <option disabled selected value> -- Destination -- </option>
                     @foreach($town as $t)
                         <option value="{{ $t->town_id }}">
                                 {{$t->town_name}}
                         </option> 
                     @endforeach 
     </select>
</div>
     @if($errors->has('destination_id'))
         <div class="text-danger">
              {{ $errors->first('destination_id')}}
         </div>
     @endif                          

    <div class="row">
     <div class="col-sm-6">
        <label>Date</label>
           <select class="form-control" name="day" id="day">
                <option disabled selected value> -- Date -- </option>
           </select>
     </div>
         @if($errors->has('day'))
               <div class="text-danger">
                    {{ $errors->first('day')}}
               </div>
         @endif

Ajax

<script>
   $(document).ready(function() {
    $('#day').click(function(){
        var selected_departure = $('#departure_id').val();
        var selected_destination = $('#destination_id').val();

        $.get('/order_trip?departure_id=' +selected_departure+'&destination_id=' 
          +selected_destination, function(data){
              $('#day').empty();
              $.each(data, function(index, value){
                console.log(value.schedule);
                $('#day').append('<option 
                   value='+value.schedule+'>'+value.schedule+'</option>');
              });
        });  
    });
});
</script>

1 Answer 1

1

You could use an accessor to format your date. Documentation

public function getScheduleAttribute($value)
{
   return $value->format('d F Y');
}

Update

And don't forget to cast your field to date time Documentation

protected $casts = [
        'schedule' => 'datetime',
    ];

And if needed set a mutator Documentation

public function setScheduleAttribute($value)
    {
        $this->attributes['schedule'] = Carbon::parse($value);
    }
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your answer. But where should I write these codes? I am sorry, I am still learning
Into the model -> Trip.php
Error "Call to a member function format() on string "
Did you add the $casts?
Yes, I did add the $casts
|

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.