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'];
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community