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']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 准备学习小程序搭建,谁能手把手的教我啊?
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥100 AT89C52单片机C语言调试之后再回答
  • ¥15 AT89C52单片机C语言串口助手发送数据包返回值
  • ¥15 C++数组中找第二小的数字程序纠错
  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)