bozboz / media-library
Requires
- php: >=5.4.0
- bozboz/admin: 0.4.*
- thapp/jitimage: 0.1.*
This package is auto-updated.
Last update: 2021-06-04 10:21:22 UTC
README
Overview
This package provides the ability to relate a piece of Media to any model via a polymorphic join table. Once correctly configured, create/edit views for the affected models will render the MediaBrowser field. The MediaBrowser field simply presents the user with existing Media to mark as being related.
Installation
- Add bozboz/media-library to
composer.json
:
"require": {
"bozboz/media-library": "0.2.*"
}
- Add the following to the
providers
array withinapp/config/app.php
:
'Bozboz\MediaLibrary\MediaLibraryServiceProvider',
'Thapp\JitImage\JitImageServiceProvider'
- Publish bozboz/media-library assets, migratons and configuration:
./artisan config:publish bozboz/media-library
./artisan asset:publish bozboz/media-library
./artisan migrate:publish bozboz/media-library
-
Run
./artisan migrate
-
Run
./artisan asset:publish bozboz/media-library
-
Run
gulp
-
Run
./artisan config:publish thapp/jitimage
-
Open
app/config/packages/thapp/jitimage/config.php
and update with the following:
use Bozboz\MediaLibrary\ImageFilters\Resize;
use Bozboz\MediaLibrary\ImageFilters\ResizeAndCrop;
return [
...
'recipes' => [
'library' => new Resize(150, 0),
'thumb' => new ResizeAndCrop(150, 150)
]
];
Making models "mediable"
By default, only Pages will be mediable out of the box. You can define new media models inside app/config/packages/bozboz/media-library/config.php
:
'models' => [
'Bozboz\Admin\Models\Page'
]
General information
This works by listening out for when a create/edit form is being built by using the MediaEventHandler subscriber. If the form being rendered is associated with a mediable model, a select box containing available Media is displayed to the user.
To get all Media instances for a given Page model, pass the Page model to Media::forModel(). This will return an instancce of Illuminate\Database\Eloquent\Relations\MorphToMany which contains the results. Actual model instances can be accessed by calling get() on the return value.