Support the ongoing development of Laravel.io →
Database Eloquent

Hi guys,

I need to run something like this in the mode. What I need to do is check if the date on the product has expired and set them to expired. The ideas of this is to run this code every time the model Products is ran. This is because I will need to pull products all over the website and I do not want to have to run this code in every controller.

I want to check every products and set its value in the database to expired if the product is expired.

This is what I have so far but I am sure it is not correct.

<?php

namespace App;
use Carbon\Carbon;

use Illuminate\Database\Eloquent\Model;

class products extends Model
{
protected $fillable = [
     	'name',
       'slug',
       'deal_type', 
       'url',
       'image', 
       'thumb', 
       'quantity',
        'customer_limit',
       'msrp',
       'start_date',
       'small_description',
        'description',
       'specs',
       'embeded_video',
        'meta_description',
       'meta_keywords',
       'votes',
        'home_page',
       'featured',
      'category',
       'views',
       'visits',
       'status',
    ];

protected $dates = ['created_at', 'start_date', 'expires_at'];


public function getExpiredAttribute()
   {
     
      $is_expired = $this->start_date->diffInDays() >= 10;
     if ($is_expired == 1) {
         $product->status = 'expired';
         $product->save();
    }
      

}



  protected $appends = ["expired'];

}
Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

virgiltu virgiltu Joined 29 Nov 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.