Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 10 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Media | |
0.00% |
0 / 10 |
|
0.00% |
0 / 3 |
20 | |
0.00% |
0 / 1 |
| getUrlAttribute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| user | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| boot | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Models; |
| 4 | |
| 5 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
| 6 | use Illuminate\Database\Eloquent\Model; |
| 7 | use Illuminate\Support\Facades\Storage; |
| 8 | |
| 9 | class Media extends Model |
| 10 | { |
| 11 | use HasFactory; |
| 12 | |
| 13 | protected $table = 'media'; |
| 14 | |
| 15 | protected $fillable = [ |
| 16 | 'name', |
| 17 | 'file_name', |
| 18 | 'mime_type', |
| 19 | 'extension', |
| 20 | 'size', |
| 21 | 'path', |
| 22 | 'disk', |
| 23 | 'alt_text', |
| 24 | 'status', |
| 25 | 'created_by', |
| 26 | ]; |
| 27 | |
| 28 | protected $appends = ['url']; |
| 29 | |
| 30 | public function getUrlAttribute() |
| 31 | { |
| 32 | return asset('storage/' . $this->path); |
| 33 | } |
| 34 | |
| 35 | public function user() |
| 36 | { |
| 37 | return $this->belongsTo(User::class, 'created_by'); |
| 38 | } |
| 39 | |
| 40 | protected static function boot() |
| 41 | { |
| 42 | parent::boot(); |
| 43 | |
| 44 | static::creating(function ($query) { |
| 45 | $query->created_by = auth()->id(); |
| 46 | }); |
| 47 | |
| 48 | static::deleting(function ($media) { |
| 49 | if (Storage::disk($media->disk)->exists($media->path)) { |
| 50 | Storage::disk($media->disk)->delete($media->path); |
| 51 | } |
| 52 | }); |
| 53 | } |
| 54 | } |