From e9b4760cffed480fc0d2fbc9cd7c93570ada0993 Mon Sep 17 00:00:00 2001 From: Farhan Israq Date: Sat, 27 Mar 2021 13:57:42 +0600 Subject: [PATCH 1/8] update: attributes docs --- README.md | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 8b064c7..0b42dd9 100644 --- a/README.md +++ b/README.md @@ -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 @@ -152,20 +152,23 @@ $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 +``` ### Update media From af13dda333c4279284a3cf7b4284004680eff02e Mon Sep 17 00:00:00 2001 From: Farhan Israq Date: Sat, 27 Mar 2021 14:00:30 +0600 Subject: [PATCH 2/8] add: appended attributes --- src/Models/Media.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Models/Media.php b/src/Models/Media.php index dc1b8af..dc01c07 100644 --- a/src/Models/Media.php +++ b/src/Models/Media.php @@ -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() { From 984b64905d7dd5a7ab553992a0268312691e6d34 Mon Sep 17 00:00:00 2001 From: Farhan Israq Date: Sat, 27 Mar 2021 14:44:06 +0600 Subject: [PATCH 3/8] add: media url attribute --- src/Models/Media.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Models/Media.php b/src/Models/Media.php index dc01c07..199649f 100644 --- a/src/Models/Media.php +++ b/src/Models/Media.php @@ -112,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. From d8db02c06ccdb3b5870ccc932468c35c28480d1e Mon Sep 17 00:00:00 2001 From: Farhan Israq Date: Sat, 27 Mar 2021 14:44:39 +0600 Subject: [PATCH 4/8] add: converted media url docs --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 0b42dd9..146b67d 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,15 @@ An instance of Media has the following attributes: 'collections' => object // eloquent collection ``` +Once you have the media instance, you can also retrieve the conversion 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. From 4fddab23370b9fbd3455cf8f1be8e9fa1c717953 Mon Sep 17 00:00:00 2001 From: Farhan Israq Date: Sat, 27 Mar 2021 14:45:28 +0600 Subject: [PATCH 5/8] fix: grammar --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 146b67d..3c54444 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,7 @@ An instance of Media has the following attributes: 'collections' => object // eloquent collection ``` -Once you have the media instance, you can also retrieve the conversion urls: +Once you have the media instance, you can also retrieve the converted media URLs: ```php // by id $media = Media::find(1); From 99b1e5c1797b07c7d1ccf79dac988564027b94b1 Mon Sep 17 00:00:00 2001 From: Farhan Israq Date: Sat, 27 Mar 2021 15:12:41 +0600 Subject: [PATCH 6/8] add: conversion url docs --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3c54444..1fbcc3e 100644 --- a/README.md +++ b/README.md @@ -434,10 +434,17 @@ 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 +// The 'thumb' conversion URL of the first image from the 'gallery' channel $post->getFirstMediaUrl('gallery', 'thumb'); ``` +```php +// if you have multiple media associated & need to retrieve URLs you can do it: +$media = $post->getMedia(); +// getUrl() accepts two arguments: name of the channel & name of the conversion +$mediaOneThumb = $media[0]->getUrl('gallery', 'thumb'); +``` + ## License The MIT License (MIT). Please read [License File](LICENSE.md) for more information. \ No newline at end of file From 36195546fc6027ff8274d49c7db63b4c73bf2322 Mon Sep 17 00:00:00 2001 From: Farhan Israq Date: Sat, 27 Mar 2021 15:24:55 +0600 Subject: [PATCH 7/8] update: getUrl docs --- README.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1fbcc3e..2ccac1d 100644 --- a/README.md +++ b/README.md @@ -260,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 @@ -441,8 +451,9 @@ $post->getFirstMediaUrl('gallery', 'thumb'); ```php // if you have multiple media associated & need to retrieve URLs you can do it: $media = $post->getMedia(); -// getUrl() accepts two arguments: name of the channel & name of the conversion -$mediaOneThumb = $media[0]->getUrl('gallery', 'thumb'); +// 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'); ``` From 966b76158b3d610c11e6f9111ffa0f68473e4a98 Mon Sep 17 00:00:00 2001 From: Farhan Israq Date: Sat, 27 Mar 2021 15:31:22 +0600 Subject: [PATCH 8/8] update: getFirstMediaUrl() docs & tips --- README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2ccac1d..b50e476 100644 --- a/README.md +++ b/README.md @@ -267,7 +267,7 @@ Though the original media URL is appended with the Media model, it's nice to kno $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(); +$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. @@ -285,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. @@ -444,18 +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 'thumb' conversion URL of the first image from the 'gallery' channel +// 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: +// 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. \ No newline at end of file