Reputation: 85
I have a table which has user publications, when the user publish a publications he choose a date that this publication will remains visible in the platform, i want a way to flip the state of a publication that time is come to now be shown anymore (date of publication is equal or greater then date system), i know about the cronjob
way but i wanted to ask here if there's a better way to do this because i think the cronjob
way is not good for this i will have to run a command every minute which i prefer not to do, i only need ideas not code.
Thank you.
Upvotes: 0
Views: 69
Reputation: 40681
Do not store an active
flag on the database. In your case active
is a derived attribute and derived attributes should not really be stored as columns, not to mention that since active
will be based on a date it is not functionally dependant on the primary key directly so is also in violation of the 2nd normal form. These two reasons will generally lead to data anomalies and what you're trying to do with a scheduled task is basically hack your way around those anomalies.
My suggested approach is to use an eloquent model attribute and attribute mutators like below:
class Publication extends Model {
protected $appends = [ 'active' ];
public function getActiveAttribute() {
return $this->activeUntil < Carbon::now(); //Still active
}
}
This way your model includes the active
attribute which is computed by eloquent and not stored in the database.
Upvotes: 2