You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Yajra表格:过期日期大于当前日期时显示EXPIRED标签

Solution for Yajra DataTables Expired Label

Let's fix your logic step by step. The main issues with your current code are:

  • Your condition checks for a string "Expired" instead of comparing the date with the current date
  • The else block is empty, so no content is shown when the condition isn't met
  • You need to ensure your date field is properly cast to a Carbon instance for easy comparisons

Step 1: Ensure Date Casting in Your Model

First, make sure your model casts the do_expiry_date field to a datetime (Carbon instance). Open your model file and add one of these:

// Option 1: Using $dates property
protected $dates = ['do_expiry_date'];

// Option 2: Using $casts property (recommended for Laravel 8+)
protected $casts = [
    'do_expiry_date' => 'datetime',
];

This lets Laravel automatically convert the database date value into a Carbon object, making date comparisons straightforward.

Step 2: Fix the DataTables Column Logic

Now update your addColumn('do-expire') block. Based on your requirement, we'll compare the expiry date with the current date.

Wait a quick note: Usually, "expired" means the date has passed (i.e., do_expiry_date < current date). But you mentioned showing the label when do_expiry_date > current date—I'll include both scenarios so you can pick the right one.

Scenario 1: Show EXPIRED when expiry date is in the future (as per your original request)

->addColumn('do-expire', function ($result) {
    // Check if we have a valid Carbon date
    if ($result->do_expiry_date instanceof \Carbon\Carbon) {
        // Compare expiry date with current date/time
        if ($result->do_expiry_date->gt(\Carbon\Carbon::now())) {
            return '<span class="label label-rejected" title="Expired">EXPIRED</span>';
        } else {
            // Show formatted date if not expired
            return $result->do_expiry_date->format('Y-m-d H:i:s'); // Adjust format as needed
        }
    } else {
        // Handle cases where the date is invalid (e.g., stored as "Expired" string)
        return '<span class="label label-rejected" title="Expired">EXPIRED</span>';
    }
})

Scenario 2: Show EXPIRED when expiry date has passed (common use case)

If you meant to show the label when the expiry date is in the past, replace gt() with lt():

if ($result->do_expiry_date->lt(\Carbon\Carbon::now())) {
    // Show expired label
}

Step 3: Full Updated Code

Here's the complete DataTables code with the fixes:

return DataTables::of($result)
    ->addIndexColumn()
    ->addColumn('action', function ($result) {
        return '<a data-id="' . $result->id . '" data-ref="' . $result->mbl_number . '" class="btn btn-xs btn-primary view_bl" data-toggle="tooltip" data-placement="bottom" title="View"><i class="fa fa-eye"></i></a>';
    })
    ->addColumn('do-expire', function ($result) {
        if ($result->do_expiry_date instanceof \Carbon\Carbon) {
            // Use gt() for future dates, lt() for past dates
            if ($result->do_expiry_date->lt(\Carbon\Carbon::now())) {
                return '<span class="label label-rejected" title="Expired">EXPIRED</span>';
            } else {
                return $result->do_expiry_date->format('Y-m-d');
            }
        } else {
            return '<span class="label label-rejected" title="Expired">EXPIRED</span>';
        }
    })
    ->rawColumns(['action', 'do-expire'])
    ->make(true);

Key Notes:

  • \Carbon\Carbon::now() uses the current date and time. If you want to compare against the current date only (ignoring time), use \Carbon\Carbon::today() instead.
  • Adjust the date format ('Y-m-d') to match your preferred display format.
  • The rawColumns() method is already correctly including 'do-expire', so the HTML label will render properly.

内容的提问来源于stack exchange,提问作者ger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 17:27:52