Skip to content

Commit 06eef2e

Browse files
authored
Merge pull request #30 from FarhanShares/media-attribute
Media attribute
2 parents 952fa06 + 420f841 commit 06eef2e

File tree

2 files changed

+51
-31
lines changed

2 files changed

+51
-31
lines changed

README.md

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,11 @@ An instance of Media has the following attributes:
135135
- id
136136
- name
137137
- file_name
138+
- extension
139+
- type
140+
- mime_type
138141
- size (in bytes)
139142
- friendly_size (in human readable format)
140-
- mime_type
141143
- url
142144
- disk
143145
- data
@@ -156,9 +158,9 @@ $media->data = ['additional_data' => 'new additional data']
156158
$media->save()
157159
```
158160

159-
**Note:**: Updating file name & disk will be added soon.
161+
**Note:** Updating file name & disk will be added soon.
160162

161-
**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.
163+
**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.
162164

163165

164166

@@ -201,7 +203,7 @@ This will establish the relationship between your model and the media model.
201203

202204
Once done, you can associate media to the model as demonstrated below.
203205

204-
The first parameter of the attachMedia() method can either be a media model / id or an iterable collection of models / ids.
206+
The first parameter of the `attachMedia()` method can either be a media model / id or an iterable collection of models / ids.
205207

206208
```php
207209
$post = Post::first();
@@ -215,31 +217,6 @@ $post->attachMedia($media, 'featured-image');
215217

216218
`attachMedia()` returns number of media attached (int) on success & null on failure.
217219

218-
### Disassociate media
219-
You can use detachMedia() to disassociate media from model.
220-
221-
```php
222-
// Detach all media from all channels
223-
$post->detachMedia();
224-
225-
// Detach the specified media
226-
$post->detachMedia($media); // or 1 or [1, 2, 3] or collection of media models
227-
228-
// Detach all media of the default channel
229-
$post->clearMediaChannel();
230-
231-
// Detach all media of the specific channel
232-
$post->clearMediaChannel('channel-name');
233-
```
234-
235-
`detachMedia()` returns number of media detached (int) on success & null on failure.
236-
237-
### Synchronize association / disassociation
238-
WIP: This feature will be added soon.
239-
240-
241-
242-
243220
### Retrieve media of a model
244221
Apart from that, `HasMedia` trait enables your app models retrieving media conveniently.
245222
```php
@@ -263,6 +240,28 @@ $post->getFirstMediaUrl();
263240
$post->getFirstMediaUrl('avatar');
264241
```
265242

243+
### Disassociate media
244+
You can use `detachMedia()` to disassociate media from model.
245+
246+
```php
247+
// Detach the specified media
248+
$post->detachMedia($media); // or 1 or [1, 2, 3] or collection of media models
249+
250+
// Detach all media from all channels
251+
$post->detachMedia();
252+
253+
// Detach all media of the default channel
254+
$post->clearMediaChannel();
255+
256+
// Detach all media of the specific channel
257+
$post->clearMediaChannel('channel-name');
258+
```
259+
260+
`detachMedia()` returns number of media detached (int) on success & null on failure.
261+
262+
### Synchronize association / disassociation
263+
WIP: This feature will be added soon.
264+
266265

267266
-----
268267
## Collections

src/Models/Media.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Media extends Model
2121
* @var array
2222
*/
2323
protected $fillable = [
24-
'disk', 'display_name', 'name', 'mime_type', 'size', 'data'
24+
'name', 'file_name', 'mime_type', 'size', 'disk', 'data'
2525
];
2626

2727
protected $casts = [
@@ -31,7 +31,7 @@ class Media extends Model
3131

3232
public static function booted()
3333
{
34-
static::deleting(static function ($media) {
34+
static::deleted(static function ($media) {
3535
// delete the media directory
3636
$deleted = Storage::disk($media->disk)->deleteDirectory($media->getDirectory());
3737
// if failed, try deleting the file then
@@ -80,6 +80,27 @@ public function isOfType(string $type)
8080
return $this->type === $type;
8181
}
8282

83+
/**
84+
* Get the file size in human readable format.
85+
*
86+
* @return string|null
87+
*/
88+
public function getFriendlySizeAttribute()
89+
{
90+
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
91+
92+
if ($this->size == 0) {
93+
return '0 ' . $units[1];
94+
}
95+
96+
for ($i = 0; $this->size > 1024; $i++) {
97+
$this->size /= 1024;
98+
}
99+
100+
return round($this->size, 2) . ' ' . $units[$i];
101+
}
102+
103+
83104
/**
84105
* Get the url to the file.
85106
*

0 commit comments

Comments
 (0)