dqwh1218 2016-04-24 13:19
浏览 39
已采纳

如果给定的模型实例已被软删除,则将类应用于yajra / datatable中的行

In yajra/laravel-datatables, I want to determine that current model is soft-deleted or not. if it is deleted , a success bootstrap class apply to row containing that model details.

this is my backend code:

$courses =
            Course::select(['course_id', 'title', 'start_date', 'end_date', 'picture', 'lesson_count', 'status', 'active', 'teacher','start_date','end_date','reg_start_date','reg_end_date']);

        if ($request->has('showDeleted') && $request->get('showDeleted') == 1) {
            $courses = $courses->withTrashed();
        }

        $datatable = app('datatables')->of($courses)
            ->orderBy('created_at', 'desc')
            //columns come here
            ->setRowClass(function ($course) {
                return ($course->trashed() ? 'danger' : 'sdasd');
            });

        return $datatable->make(true);

As you see I used:

->setRowClass(function ($course) {
                    return ($course->trashed() ? 'danger' : ' ');
                });

to apply desired class But $course->trashed return false always for all model instances even those is not trashed.

what is best solution?

  • 写回答

1条回答 默认 最新

  • duanchun6148 2016-04-25 05:50
    关注

    I found the solution. It was my mistake that I should included deleted_at field in columns selection on select method:

    $courses =
    Course::select(['course_id', 'title', 'start_date', 'end_date', 'picture', 'lesson_count', 'status', 'active', 'teacher','start_date','end_date','reg_start_date','reg_end_date','deleted_at']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?