gterrusa / frontendmedialibrary
:package_description
Installs: 2 281
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- illuminate/support: >=7.0
- spatie/laravel-medialibrary: >=7.0
Requires (Dev)
- orchestra/testbench: ~5|~6
- phpunit/phpunit: ~9.0
README
FrontendMedialibrary is a trait that makes the media, on your Laravel models that use Spatie Medialibrary, more easily accessible in frontend frameworks. Take a look at contributing.md to see a to do list.
Installation
Via Composer
$ composer require gterrusa/frontendmedialibrary
Usage
Set up your Model
/**
* Model must be using Spatie Medialibrary
*/
class User extends Model implements HasMedia
{
use InteractsWithMedia, HasFrontendMedia;
// If you would like the frontendMedia attribute included with your model automatically
protected $appends = ['frontendMedia'];
/**
* Spatie medialibrary media collections
* any media collections can be set up here
* this is just an example.
*/
public function registerMediaCollections(): void
{
$this->addMediaCollection('avatar')->singleFile();
$this->addMediaCollection('user_gallery');
}
/**
* Spatie medialibrary media conversions
* any media conversions can be set up here
* this is just an example
*/
public function registerMediaConversions(Media $media = null): void
{
$this->addMediaConversion('optimized')
->fit(Manipulations::FIT_MAX, 1500, 1500);
$this->addMediaConversion('thumb')
->fit(Manipulations::FIT_MAX, 250, 250);
}
}
Example Result:
** HasFrontendMedia will pick up all of your registered collections, conversions, and custom properties automatically. They do not have to be these exact ones. This is just an example of the structure of the output.
>>> $user->frontendMedia;
=> Illuminate\Support\Collection {#4382
all: [
"avatar" => Illuminate\Support\Collection {#4439
all: [
[
"src" => "http://frontend-medialibrary.local/storage/1/41301408.jpeg",
"conversions" => Illuminate\Support\Collection {#4425
all: [
"thumb" => "http://frontend-medialibrary.local/storage/1/conversions/41301408-thumb.jpg",
"optimized" => "http://frontend-medialibrary.local/storage/1/conversions/41301408-optimized.jpg",
],
},
"custom_properties" => Illuminate\Support\Collection {#4384
all: [
"alt" => "my alt tag",
],
},
],
],
},
"user_gallery" => Illuminate\Support\Collection {#4469
all: [
[
"src" => "http://frontend-medialibrary.local/storage/2/49268400_10155665639226152_4845558111659884544_n.jpg",
"conversions" => Illuminate\Support\Collection {#4443
all: [
"thumb" => "http://frontend-medialibrary.local/storage/2/conversions/49268400_10155665639226152_4845558111659884544_n-thumb.jpg",
"optimized" => "http://frontend-medialibrary.local/storage/2/conversions/49268400_10155665639226152_4845558111659884544_n-optimized.jpg",
],
},
"custom_properties" => Illuminate\Support\Collection {#4456
all: [],
},
],
[
"src" => "http://frontend-medialibrary.local/storage/3/68544499_10156079858431152_5954628979427115008_n.jpg",
"conversions" => Illuminate\Support\Collection {#4444
all: [
"thumb" => "http://frontend-medialibrary.local/storage/3/conversions/68544499_10156079858431152_5954628979427115008_n-thumb.jpg",
"optimized" => "http://frontend-medialibrary.local/storage/3/conversions/68544499_10156079858431152_5954628979427115008_n-optimized.jpg",
],
},
"custom_properties" => Illuminate\Support\Collection {#4470
all: [],
},
],
],
},
],
}
Change log
Please see the changelog for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email author email instead of using the issue tracker.
Credits
License
license. Please see the license file for more information.