1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models\enso\howto; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Http\UploadedFile; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
use App\Contracts\enso\files\Attachable; |
9
|
|
|
use App\Traits\enso\files\HasFile; |
10
|
|
|
use LaravelEnso\Helpers\Traits\CascadesMorphMap; |
11
|
|
|
use LaravelEnso\HowTo\Exceptions\Video as Exception; |
12
|
|
|
|
13
|
|
|
class Video extends Model implements Attachable |
14
|
|
|
{ |
15
|
|
|
use CascadesMorphMap, HasFile; |
|
|
|
|
16
|
|
|
|
17
|
|
|
protected $table = 'how_to_videos'; |
18
|
|
|
|
19
|
|
|
protected $guarded = ['id']; |
20
|
|
|
|
21
|
|
|
protected string $folder = 'howToVideos'; |
22
|
|
|
|
23
|
|
|
public function poster() |
24
|
|
|
{ |
25
|
|
|
return $this->hasOne(Poster::class); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function tags() |
29
|
|
|
{ |
30
|
|
|
return $this->belongsToMany( |
31
|
|
|
Tag::class, |
32
|
|
|
'how_to_tag_how_to_video', |
33
|
|
|
'how_to_video_id', |
34
|
|
|
'how_to_tag_id' |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function store(UploadedFile $file, array $attributes) |
39
|
|
|
{ |
40
|
|
|
if (self::whereHas('file', fn ($query) => $query |
41
|
|
|
->whereOriginalName($file->getClientOriginalName()))->exists()) { |
42
|
|
|
throw Exception::exists(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return DB::transaction(function () use ($file, $attributes) { |
46
|
|
|
$video = $this->create($attributes); |
47
|
|
|
|
48
|
|
|
return tap($video)->upload($file); |
49
|
|
|
}); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function syncTags($tagList) |
53
|
|
|
{ |
54
|
|
|
$this->tags()->sync($tagList); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function tagList() |
58
|
|
|
{ |
59
|
|
|
return $this->tags()->pluck('id'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function delete() |
63
|
|
|
{ |
64
|
|
|
optional($this->poster)->delete(); |
65
|
|
|
|
66
|
|
|
parent::delete(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|