将日期函数传递给Laravel中的javascript

时间:2019-04-06 17:27:29

标签: javascript ajax laravel

我正在使用ajax获取一些数据,但是我无法通过diffForHuman()函数来获取不同格式的日期。我想要另一种格式的日期。但是通过将created_at传递给我的标记,就给了我不确定的日期。下面是我的代码。请帮助

// Javascript

        $(document).ready(function () {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
        });

        $('select[name="class_id"]').on('change', function() {
            var classID = $(this).val();
            if(classID) {

                $.ajax({

                    url: '/attendance/ajax/'+classID,
                    type: "GET",
                    dataType: "json",
                    success:function(data) {

                        var markup = '';
                        markup += '<tr><th style="width: 2%" class="align-middle text-center"><input type="checkbox" id="options"></th><th style="width: 2%" class="align-middle text-center">#</th> <th style="width: 15%" class="text-center">Student ID<input type="text" class="form-control" disabled></th> <th style="width: 15%" class="text-center">Student Name<input type="text" class="form-control" disabled></th> <th style="width: 15%" class="text-center">Attendance<input type="text" class="form-control" disabled></th> <th style="width: 15%" class="text-center">Date<input type="text" class="form-control" disabled></th> <th style="width: 15%;" class="align-middle text-center">Actions</th> <tr>';

                        $.each(data, function(key, value) {

                            markup += '<tr> <td><input class="checkBoxes" type="checkbox" name="checkBoxArray[]"></td> <td><input type="hidden" value="'+value.id+'" name="id[]">' + value.id + '</td> <td><input type="hidden" value="'+value.student_id+'" name="student_id[]">' + value.student_id + '</td> <td><input type="hidden" value="'+value.first_name+'" name="first_name[]"><input type="hidden" value="'+value.last_name+'" name="last_name[]">' + value.first_name+ ' '  + value.last_name +  '<td><input type="hidden" value="'+value.attendance+'" name="attendance[]">' + value.attendance + '</td>' +  '<td><input type="hidden" value="'+value.created_at+'" name="created_at[]">' + value.created_at + '</td>' + '<td style=" width=12%" class="text-center"> <a><button title="Edit" class="btn btn-outline-primary"><span class="fas fa-pencil-alt"></span></button></a> </td>' +  '</td> <tr>';

                        });
                        $('table[id="studentsData"]').html(markup);
                    }
                });
            }
        });
    });

//控制器

 public function student_attendance_registers() {
        $attendances = StudentsAttendance::all();
        $classes = StudentsClass::pluck('class_name', 'id')->all();

        return view('admin.students.attendance.student_attendance_registers', compact('attendances', 'classes'));
    }




 //Model

    class StudentsAttendance extends Model
    {
        protected $fillable = [
            'class_id',
            'student_id',
            'first_name',
            'last_name',
            'attendance'
        ];

        public function studentsClass() {
            return $this->belongsTo('App\StudentsClass');
        }

        public function getdateForHumansAttribute()
        {
            return $this->created_at->diffForHumans();
        }

        public function toArray()
        {
            $data = parent::toArray();

            $data['diffForHumans'] = $this->diffForHumans;

            return $data;
        }

    }

1 个答案:

答案 0 :(得分:0)

最好的方法是添加一个getdateForHumanAttribute方法:

public function getdateForHumansAttribute()
{
    return $this->created_at->diffForHumans();
}

然后在模型上可以在任何地方使用

$model->dateForHumans;

对于在检索此模型时始终添加dateForHumans属性的简便方法,请将其添加到toArray方法中:
(也在您的模型上)

public function toArray()
{
    $data = parent::toArray();

    $data['diffForHumans'] = $this->diffForHumans;

    return $data;
}