Skip to content

Commit

Permalink
Merge pull request #33 from FarhanShares/media-attribute-hotfix
Browse files Browse the repository at this point in the history
Media attributes hotfix & updated docs
  • Loading branch information
FarhanShares authored Mar 27, 2021
2 parents bba11d4 + 966b761 commit 8868da1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 16 deletions.
68 changes: 52 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ MediaMan supports all of the storage drivers that are supported by Laravel. For
// update the disk config to use our recently created media disk
'disk' => 'media'
```

Now, run `php artisan storage:link` to create the symbolic link of our newly created media disk.


## Media
Expand Down Expand Up @@ -152,21 +152,33 @@ $media = Media::with('collections')->find(1);
```

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)
- url
- disk
- data
- created_at
- updated_at
- collections

```php
'id' => int
'name' => string
'file_name' => string
'extension' => string
'type' => string
'mime_type' => string
'size' => int // in bytes
'friendly_size' => string // in human readable format
'media_url' => string // original media url
'disk' => string
'data' => array // casts as array
'created_at' => string
'updated_at' => string
'collections' => object // eloquent collection
```

Once you have the media instance, you can also retrieve the converted media URLs:
```php
// by id
$media = Media::find(1);
// original media url
$media->getUrl()
// converted media url
$media->getUrl('conversion-name');
```

### Update media
You can update a media name with an instance of Media.
Expand Down Expand Up @@ -248,6 +260,16 @@ $post->getMedia();
$post->getMedia('featured-image');
```


Though the original media URL is appended with the Media model, it's nice to know that you have a getUrl() method available.

```php
$media = $post->getMedia('featured-image');
// getUrl() accepts only one optional argument: name of the conversion
// leave it empty to get the original media URL
$mediaOneUrl = $media[0]->getUrl();
```

It might be a common scenario for most of the Laravel apps to use the first media item more often, hence MediaMan has dedicated methods to retrieve the first item among all associated media.

```php
Expand All @@ -263,6 +285,7 @@ $post->getFirstMediaUrl();
// URL of the first media item from the specified channel
$post->getFirstMediaUrl('featured-image');
```
*Tip:* getFirstMediaUrl() accepts two optional arguments: channel name & conversion name

### Disassociate media
You can use `detachMedia()` method which is also shipped with HasMedia trait to disassociate media from model.
Expand Down Expand Up @@ -422,10 +445,23 @@ class Post extends Model
From now on, whenever a media item is attached to the "gallery" channel, a converted image will be generated. You can get the url of the converted image as demonstrated below:

```php
// The thumbnail of the first image from the gallery group
// getFirstMediaUrl() accepts two optional arguments: channel name & conversion name
// you should provide channel name & conversion name to get the url
$post->getFirstMediaUrl('gallery', 'thumb');
```

*Tip:* The default channel name is `default`.


```php
// if you have multiple media associated & need to retrieve URLs you can do it with getUrl():
$media = $post->getMedia();
// getUrl() accepts only one optional argument: name of the conversion
// you should provide the conversion name to get the url
$mediaOneThumb = $media[0]->getUrl('thumb');
```

*Tip:* The `media_url` is always appended & it's the original media URL.

## License
The MIT License (MIT). Please read [License File](LICENSE.md) for more information.
21 changes: 21 additions & 0 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,22 @@ class Media extends Model
'name', 'file_name', 'mime_type', 'size', 'disk', 'data'
];

/**
* The attributes that need casting.
*
* @var array
*/
protected $casts = [
'data' => Json::class
];

/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['friendly_size', 'media_url', 'type', 'extension'];


public static function booted()
{
Expand Down Expand Up @@ -100,6 +112,15 @@ public function getFriendlySizeAttribute()
return round($this->size, 2) . ' ' . $units[$i];
}

/**
* Get the original media url.
*
* @return string
*/
public function getMediaUrlAttribute()
{
return $this->filesystem()->url($this->getPath());
}

/**
* Get the url to the file.
Expand Down

0 comments on commit 8868da1

Please sign in to comment.