*/ protected $fillable = [ 'date', 'quantity', ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $model) { $campaign = Campagne::withTrashed()->where('camp_id', $model->archive_campaign_id)->firstOrFail(); $archiveCampaign = ArchiveCampaign::query()->firstOrCreate( [ 'campaign_id' => $model->archive_campaign_id ], [ 'name' => $campaign->camp_name, 'archive_site_id' => $campaign->camp_siteId, ] ); $model->archiveCampaign()->associate($archiveCampaign); $coupon = Coupon::withTrashed()->where('coupon_id', $model->archive_coupon_id)->firstOrFail(); $archiveCoupon = ArchiveCoupon::query()->firstOrCreate( [ 'coupon_code' => $model->archive_coupon_id ], [ 'name' => $coupon->coupon_code, ] ); $model->archiveCoupon()->associate($archiveCoupon); }); static::updating(function () { throw new \RuntimeException('Upating an archived stat is not authorized'); }); static::deleting(function () { throw new \RuntimeException('Deleting an archived stat is not authorized'); }); } // * RELATIONS /** * ArchiveCoupon model relation * @return Illuminate\Database\Eloquent\Relations\BelongsTo */ public function archiveCoupon() { return $this->belongsTo(ArchiveCoupon::class); } /** * ArchiveCampaign model relation * @return Illuminate\Database\Eloquent\Relations\BelongsTo */ public function archiveCampaign() { return $this->belongsTo(ArchiveCampaign::class); } }