【问题标题】:Laravel Ajax display datetime with other formatLaravel Ajax 以其他格式显示日期时间
【发布时间】:2020-04-28 15:46:42
【问题描述】:

我有表'Trip',其中一个字段是'schedule'。 'schedule' 的类型是日期时间格式。 我在 laravel 中创建了一个项目。在组合框中,它显示“2020-04-29 10:09:00”,但我希望它显示为“2020 年 4 月 29 日”。 你知道如何解决它吗?谢谢你。 这是我的代码。

订单控制器:

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

在 web.php 中

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

在 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

阿贾克斯

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

【问题讨论】:

    标签: mysql ajax laravel datetime


    【解决方案1】:

    您可以使用访问器来格式化您的日期。 Documentation

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

    更新

    并且不要忘记将您的字段转换为日期时间Documentation

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

    如果需要,设置一个 mutator Documentation

    public function setScheduleAttribute($value)
        {
            $this->attributes['schedule'] = Carbon::parse($value);
        }
    

    【讨论】:

    • 感谢您的回答。但是我应该在哪里写这些代码呢?对不起,我还在学习中
    • 进入模型-> Trip.php
    • 错误“调用字符串上的成员函数 format()”
    • 你添加了 $casts 吗?
    • 是的,我确实添加了 $casts
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 2015-08-06
    • 2020-04-14
    • 2014-04-15
    • 1970-01-01
    • 2017-04-03
    相关资源
    最近更新 更多