3 namespace BookStack\Entities\Models;
5 use BookStack\Auth\User;
6 use BookStack\Interfaces\Deletable;
7 use BookStack\Interfaces\Loggable;
8 use Illuminate\Database\Eloquent\Model;
9 use Illuminate\Database\Eloquent\Relations\BelongsTo;
10 use Illuminate\Database\Eloquent\Relations\MorphTo;
13 * @property Deletable $deletable
15 class Deletion extends Model implements Loggable
18 * Get the related deletable record.
20 public function deletable(): MorphTo
22 return $this->morphTo('deletable')->withTrashed();
26 * Get the user that performed the deletion.
28 public function deleter(): BelongsTo
30 return $this->belongsTo(User::class, 'deleted_by');
34 * Create a new deletion record for the provided entity.
36 public static function createForEntity(Entity $entity): self
38 $record = (new self())->forceFill([
39 'deleted_by' => user()->id,
40 'deletable_type' => $entity->getMorphClass(),
41 'deletable_id' => $entity->id,
48 public function logDescriptor(): string
50 $deletable = $this->deletable()->first();
52 if ($deletable instanceof Entity) {
53 return "Deletion ({$this->id}) for {$deletable->getType()} ({$deletable->id}) {$deletable->name}";
56 return "Deletion ({$this->id})";
60 * Get a URL for this specific deletion.
62 public function getUrl($path): string
64 return url("/settings/recycle-bin/{$this->id}/" . ltrim($path, '/'));