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 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分