Skip to content

Commit

Permalink
Merge pull request #30 from FarhanShares/media-attribute
Browse files Browse the repository at this point in the history
Media attribute
  • Loading branch information
FarhanShares authored Mar 26, 2021
2 parents 952fa06 + 420f841 commit 06eef2e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 31 deletions.
57 changes: 28 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.



Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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
Expand Down
25 changes: 23 additions & 2 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 06eef2e

Please sign in to comment.