Home > Software engineering >  How to count days and trigger something after passing certain days in database
How to count days and trigger something after passing certain days in database

Time:12-02

Im creating this website using laravel and there is an option to add new items to the sell when an item adds the admin can mark it as a new arrival, I want to count 10 days from the date added and when the 10 days passed, I want to item no longer listed as new arrival. How can I implement this

CodePudding user response:

I would suggest to consider those 2 options:

via Schedule command Reference-doc

  • Create command/use closure which will query database and update items
  • Run this command everyday/every hour

via Job Reference-doc

  • Create job which will query database and update status
  • Once your item is created you can dispatch job with a delay of 10 days.

CodePudding user response:

// Product Model
public function isNew()
{
    return $this->created_at->diffInDays(Carbon::now()) <= 10;
}

// Product Controller
public function index()
{
    $products = Product::all();
    return view('products.index', compact('products'));
}

// Product View
@foreach($products as $product)
    @if($product->isNew())
        <span >New</span>
    @endif
@endforeach

I think you are looking for something with this logic!

  • Related