*/ protected $fillable = [ 'name', 'campaign_id', 'archive_site_id' ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $model) { $model->assertFieldIsUnique('campaign_id', $model->campaign_id); $site = Site::query()->where('site_id', $model->archive_site_id)->firstOrFail(); $archiveSite = ArchiveSite::query()->firstOrCreate( [ 'site_id' => $model->archive_site_id ], [ 'name' => $site->site_nom ] ); $model->archiveSite()->associate($archiveSite); }); static::updating(function () { throw new \RuntimeException('Updating an archived campaign is not authorized'); }); static::deleting(function () { throw new \RuntimeException('Deleting an archived campaign is not authorized'); }); } // * RELATIONS /** * ArchiveSite model relation * @return Illuminate\Database\Eloquent\Relations\BelongsTo */ public function archiveSite() { return $this->belongsTo(ArchiveSite::class); } /** * ArchiveSite model relation * @return Illuminate\Database\Eloquent\Relations\HasMany */ public function archiveStat() { return $this->hasMany(ArchiveStat::class); } }