'date:Y-m-d', 'end_diffusion_date' => 'date:Y-m-d', 'end_validity_date' => 'date:Y-m-d', ]; public function campaigns() : BelongsToMany { return $this->belongsToMany(Campaign::class); } /** * The "booted" method of the model. */ protected static function booted(): void { static::creating(function (Coupon $coupon) { $coupon->can_be_used = $this->checkIfCanBeUsed(); }); static::updating(function (Coupon $coupon) { $coupon->can_be_used = $this->checkIfCanBeUsed(); }); } public function checkIfCanBeUsed() { if ( (is_null($this->start_diffusion_date) or $this->start_diffusion_date->lessThanOrEqualTo(Carbon::now())) and (is_null($this->end_diffusion_date) or $this->end_diffusion_date->greaterThanOrEqualTo(Carbon::now())) and (is_null($this->end_validity_date) or $this->end_validity_date->greaterThanOrEqualTo(Carbon::now()->addDays($this->validity_period))) ){ return true; } // FIXME : Ajouter calendrier de diffusion et le prendre en compte dans la condition return false; } }