]> BookStack Code Mirror - bookstack/blob - app/Uploads/Image.php
Added method for using enity ownership in relation queries
[bookstack] / app / Uploads / Image.php
1 <?php
2
3 namespace BookStack\Uploads;
4
5 use BookStack\Entities\Models\Page;
6 use BookStack\Model;
7 use BookStack\Traits\HasCreatorAndUpdater;
8 use Illuminate\Database\Eloquent\Factories\HasFactory;
9
10 /**
11  * @property int    $id
12  * @property string $name
13  * @property string $url
14  * @property string $path
15  * @property string $type
16  * @property int    $uploaded_to
17  * @property int    $created_by
18  * @property int    $updated_by
19  */
20 class Image extends Model
21 {
22     use HasFactory;
23     use HasCreatorAndUpdater;
24
25     protected $fillable = ['name'];
26     protected $hidden = [];
27
28     /**
29      * Get a thumbnail for this image.
30      *
31      * @throws \Exception
32      */
33     public function getThumb(int $width, int $height, bool $keepRatio = false): string
34     {
35         return app()->make(ImageService::class)->getThumbnail($this, $width, $height, $keepRatio);
36     }
37
38     /**
39      * Get the page this image has been uploaded to.
40      * Only applicable to gallery or drawio image types.
41      */
42     public function getPage(): ?Page
43     {
44         return $this->belongsTo(Page::class, 'uploaded_to')->first();
45     }
46 }