]> BookStack Code Mirror - bookstack/blob - app/Entities/Deletion.php
Checked over recycle bin parent/child flows
[bookstack] / app / Entities / Deletion.php
1 <?php namespace BookStack\Entities;
2
3 use BookStack\Auth\User;
4 use Illuminate\Database\Eloquent\Model;
5 use Illuminate\Database\Eloquent\Relations\BelongsTo;
6 use Illuminate\Database\Eloquent\Relations\MorphTo;
7
8 class Deletion extends Model
9 {
10
11     /**
12      * Get the related deletable record.
13      */
14     public function deletable(): MorphTo
15     {
16         return $this->morphTo('deletable')->withTrashed();
17     }
18
19     /**
20      * The the user that performed the deletion.
21      */
22     public function deleter(): BelongsTo
23     {
24         return $this->belongsTo(User::class, 'deleted_by');
25     }
26
27     /**
28      * Create a new deletion record for the provided entity.
29      */
30     public static function createForEntity(Entity $entity): Deletion
31     {
32         $record = (new self())->forceFill([
33             'deleted_by' => user()->id,
34             'deletable_type' => $entity->getMorphClass(),
35             'deletable_id' => $entity->id,
36         ]);
37         $record->save();
38         return $record;
39     }
40
41 }