diff --git a/README.md b/README.md index 8620268..01f6265 100644 --- a/README.md +++ b/README.md @@ -135,9 +135,11 @@ An instance of Media has the following attributes: - id - name - file_name +- extension +- type +- mime_type - size (in bytes) - friendly_size (in human readable format) -- mime_type - url - disk - data @@ -156,9 +158,9 @@ $media->data = ['additional_data' => 'new additional data'] $media->save() ``` -**Note:**: Updating file name & disk will be added soon. +**Note:** Updating file name & disk will be added soon. -**Heads Up!:** Do not update anything other than `name` & `data` using the Media instance. If you need to deal with collections, please read the docs below. +**Heads Up!** Do not update anything other than `name` & `data` using the Media instance. If you need to deal with collections, please read the docs below. @@ -201,7 +203,7 @@ This will establish the relationship between your model and the media model. Once done, you can associate media to the model as demonstrated below. -The first parameter of the attachMedia() method can either be a media model / id or an iterable collection of models / ids. +The first parameter of the `attachMedia()` method can either be a media model / id or an iterable collection of models / ids. ```php $post = Post::first(); @@ -215,31 +217,6 @@ $post->attachMedia($media, 'featured-image'); `attachMedia()` returns number of media attached (int) on success & null on failure. -### Disassociate media -You can use detachMedia() to disassociate media from model. - -```php -// Detach all media from all channels -$post->detachMedia(); - -// Detach the specified media -$post->detachMedia($media); // or 1 or [1, 2, 3] or collection of media models - -// Detach all media of the default channel -$post->clearMediaChannel(); - -// Detach all media of the specific channel -$post->clearMediaChannel('channel-name'); -``` - -`detachMedia()` returns number of media detached (int) on success & null on failure. - -### Synchronize association / disassociation -WIP: This feature will be added soon. - - - - ### Retrieve media of a model Apart from that, `HasMedia` trait enables your app models retrieving media conveniently. ```php @@ -263,6 +240,28 @@ $post->getFirstMediaUrl(); $post->getFirstMediaUrl('avatar'); ``` +### Disassociate media +You can use `detachMedia()` to disassociate media from model. + +```php +// Detach the specified media +$post->detachMedia($media); // or 1 or [1, 2, 3] or collection of media models + +// Detach all media from all channels +$post->detachMedia(); + +// Detach all media of the default channel +$post->clearMediaChannel(); + +// Detach all media of the specific channel +$post->clearMediaChannel('channel-name'); +``` + +`detachMedia()` returns number of media detached (int) on success & null on failure. + +### Synchronize association / disassociation +WIP: This feature will be added soon. + ----- ## Collections diff --git a/src/Models/Media.php b/src/Models/Media.php index 43458ba..dc1b8af 100644 --- a/src/Models/Media.php +++ b/src/Models/Media.php @@ -21,7 +21,7 @@ class Media extends Model * @var array */ protected $fillable = [ - 'disk', 'display_name', 'name', 'mime_type', 'size', 'data' + 'name', 'file_name', 'mime_type', 'size', 'disk', 'data' ]; protected $casts = [ @@ -31,7 +31,7 @@ class Media extends Model public static function booted() { - static::deleting(static function ($media) { + static::deleted(static function ($media) { // delete the media directory $deleted = Storage::disk($media->disk)->deleteDirectory($media->getDirectory()); // if failed, try deleting the file then @@ -80,6 +80,27 @@ public function isOfType(string $type) return $this->type === $type; } + /** + * Get the file size in human readable format. + * + * @return string|null + */ + public function getFriendlySizeAttribute() + { + $units = ['B', 'KB', 'MB', 'GB', 'TB']; + + if ($this->size == 0) { + return '0 ' . $units[1]; + } + + for ($i = 0; $this->size > 1024; $i++) { + $this->size /= 1024; + } + + return round($this->size, 2) . ' ' . $units[$i]; + } + + /** * Get the url to the file. *