detachLocationsOnDelete(); }); } // // // protected function detachLocationsOnDelete() { if ($this->locationableIsSingleRelationType() || !$this->locationableIsMorphRelationType()) { return; } $locationable = $this->getLocationableRelationObject(); if (Igniter::runningInAdmin() && !AdminAuth::isSuperUser() && $locationable->count()) { throw new FlashException(lang('igniter::admin.alert_warning_locationable_delete')); } $locationable->detach(); } // // // public function getLocationableRelationObject() { $relationName = $this->locationableRelationName(); return $this->{$relationName}(); } public function locationableIsSingleRelationType(): bool { $relationType = $this->getRelationType($this->locationableRelationName()); return in_array($relationType, ['hasOne', 'belongsTo', 'morphOne']); } public function locationableIsMorphRelationType(): bool { $relationType = $this->getRelationType($this->locationableRelationName()); return in_array($relationType, ['morphToMany', 'belongsToMany']); } public function locationableRelationName() { return defined('static::LOCATIONABLE_RELATION') ? static::LOCATIONABLE_RELATION : 'location'; } public function locationableRelationExists(): bool { $relationName = $this->locationableRelationName(); if ($this->locationableIsSingleRelationType()) { return !is_null($this->{$relationName}); } return $this->{$relationName} instanceof Collection ? $this->{$relationName}->isNotEmpty() : ($this->{$relationName} ?? []) !== []; } }