Laravel Case Study: Create Status Providing Deadline Information

·

1 min read

Imagine you have an order system for goods. Where the ordering system has a manual approval system.

Then you are assigned by your manager to create a dashboard that displays the tempo status of customer orders that must be followed up by the admin.

Let's get started:

I created it with accessor function with name getDueStatusAttribute:

class OrderProduct extends Model
{

 //other code


public function getDueStatusAttribute()
    {
        $today = today();
        $next_three_days = now()->addDays(3);
        $next_seven_days = now()->addDays(7);
        $status = '';

        /*
            ritical = 1-3 days ahead
            warning = today + 3 - 7 days ahead
            attention = more than 7 days ahead
        */
        if ($this->request_deliver_date >= $today && $this->request_deliver_date <= $next_three_days || $this->request_deliver_date < $today) {
            $status = 'critical';
        } elseif ($this->request_deliver_date > $next_three_days && $this->request_deliver_date <= $next_seven_days) {
            $status = 'warning';
        } else {
            $status = 'attention';
        }

        return $status;
    }
}

then on the blade I use it like this:

@foreach ($orderProducts as $x => $corderProduct)
     @if ($corderProduct->due_status === 'critical')
             <h5 style="background-color: #fa0707;" class="badge badge-pill badge-warning">Critical</h5><br>
     @elseif($corderProduct->due_status === 'warning')
              <h5 style="background-color: #d36a09;" class="badge badge-pill badge-warning">Warning</h5><br>
    @elseif($corderProduct->due_status)
             <h5 style="background-color: #f1c40f;" class="badge badge-pill badge-warning">Need Attention</h5>
     @endif
@endforeach

And this is how it looks: image.png