|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models\enso\Avatars; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Http\UploadedFile; |
|
7
|
|
|
use Illuminate\Support\Facades\Auth; |
|
8
|
|
|
use Illuminate\Support\Facades\DB; |
|
9
|
|
|
use App\Models\User; |
|
10
|
|
|
use App\Contracts\enso\files\Attachable; |
|
11
|
|
|
use App\Traits\enso\files\HasFile; |
|
12
|
|
|
use LaravelEnso\Helpers\App\Traits\CascadesMorphMap; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @property int $id |
|
16
|
|
|
* @property int $user_id |
|
17
|
|
|
* @property string $created_at |
|
18
|
|
|
* @property string $updated_at |
|
19
|
|
|
* @property User $user |
|
20
|
|
|
*/ |
|
21
|
|
|
class Avatar extends Model implements Attachable |
|
22
|
|
|
{ |
|
23
|
|
|
use CascadesMorphMap, HasFile; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The table associated with the model. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $table = 'avatars'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
|
|
36
|
|
|
public const Width = 250; |
|
37
|
|
|
public const Height = 250; |
|
38
|
|
|
|
|
39
|
|
|
protected $fillable = ['user_id', 'original_name', 'saved_name']; |
|
40
|
|
|
|
|
41
|
|
|
protected $hidden = ['user_id', 'created_at', 'updated_at']; |
|
42
|
|
|
|
|
43
|
|
|
protected $casts = ['user_id' => 'int']; |
|
44
|
|
|
|
|
45
|
|
|
protected $optimizeImages = true; |
|
46
|
|
|
|
|
47
|
|
|
protected $resizeImages = [ |
|
48
|
|
|
'width' => self::Width, |
|
49
|
|
|
'height' => self::Height, |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
protected $mimeTypes = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif']; |
|
53
|
|
|
|
|
54
|
|
|
protected $folder = 'avatars'; |
|
55
|
|
|
|
|
56
|
|
|
public function user() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->belongsTo(User::class); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function store(UploadedFile $file) |
|
62
|
|
|
{ |
|
63
|
|
|
return DB::transaction(function () use ($file) { |
|
64
|
|
|
$this->delete(); |
|
65
|
|
|
$avatar = self::create(['user_id' => Auth::user()->id]); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
return tap($avatar)->upload($file); |
|
68
|
|
|
}); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|