如何在 Laravel 中显示限时产品报价

How to Display product offer for a limited period in Laravel

我需要显示,因为产品状态对于从 X 日期到 X 日期的 selected 日期是特殊的,

这就是我的 UI 的样子

在这里用户可以创建新的子类别和select特价优惠日期

这是我在控制器中的显示功能

 public function show(Category $category)
{
    // ! Search Filter
    $filter = new SearchFilter();
    $filter->where('parent_id','=',$category->id);

    // ! Date Filter (Today)
     $day = Carbon::now();
     $today = $day->toDateString();
   

    return view('manage.categories.show')->with([
        'pageTitle' => $category->name,
        'allItems' => $this->dataRepo->search([], $filter),
        'isDestroyingEntityAllowed' => $this->isDestroyingEntityAllowed,
        'entity' => $category,
        'today'=>$today,
    ]);
}

这是我的 blade 检查日期的地方

@foreach ($allItems as $item)
            <td>
                @if ($item->special_start == $today || $item->special_end == $today)
                    Special
                    @else
                    Regular
                @endif
            </td>

    @endforeach

但是只有匹配开始日期和结束日期的日期才会显示Special,开始日期和结束日期之间的天数将显示为常规.

我该如何解决?

使用Carbon不建议在view中编写逻辑,但可以将其移至控制器

@foreach ($allItems as $item)
<td>
    @php
        $first = \Carbon\Carbon::create($item->special_start);
        $second = \Carbon\Carbon::create($item->special_end);
        $diff = now()->between($first, $second);
    @endphp
    @if ($diff)
    Special
    @else
    Regular
    @endif
</td>
@endforeach

ref link https://carbon.nesbot.com/docs/#api-comparison