This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f39295
commit f89b6c9
Showing
16 changed files
with
22,877 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,34 @@ | ||
# Media Library Field | ||
This field is designed to be used with the [media library package from Spatie](https://github.com/spatie/laravel-medialibrary). It currently only supports single file uploading. Take a look at the example usage below: | ||
# Laravel Nova Media Library | ||
This package is designed to be used with the [awesome media library package from Spatie](https://github.com/spatie/laravel-medialibrary). With this package you can add an image field for uploading single files to a resource, and add an images field to resources to display all of their associated media. | ||
|
||
```php | ||
use Kingsley\MediaLibraryField\MediaLibraryField; | ||
use Kingsley\NovaMediaLibrary\Fields\Image; | ||
|
||
MediaLibraryField::make('Avatar') | ||
Image::make('Avatar') | ||
->usingConversion('thumb') | ||
->preservingOriginal() | ||
->toMediaCollection('avatar') | ||
``` | ||
|
||
In this example we're defining a field called `Avatar` that uses the `thumb` conversion as the image to be displayed (on detail and index). The other methods called are **dynamically** applied to the upload request - **this lets you call any media-library method on the field.** When updating the field above it will store the image like this: | ||
In this example we're defining a field called `Avatar` that uses the `thumb` conversion as the image to be displayed (on detail and index). The other methods called are **dynamically** applied to the upload request - **this lets you call any media-library method on the field.**. | ||
|
||
If you want it to remove the old image before uploading the new one, be sure to make your model's media collection a [single file collection](https://docs.spatie.be/laravel-medialibrary/v7/working-with-media-collections/defining-media-collections#single-file-collections). | ||
|
||
To show all media records for your resource simply add the `Images` field like so: | ||
|
||
```php | ||
// Must be uploading an image | ||
$request->validate([ | ||
$requestAttribute => 'image' | ||
]); | ||
|
||
// Kick off the media-library process | ||
$query = $model->addMedia($request[$requestAttribute]); | ||
|
||
// Call any of the media-library methods on the query | ||
foreach ($request->all() as $key => $value) { | ||
if (starts_with($key, 'ml_')) { | ||
$method = substr($key, 3); | ||
$arguments = is_array($value) ? $value : [$value]; | ||
$query->$method(...$arguments); | ||
} | ||
use Kingsley\NovaMediaLibrary\Fields\Images; | ||
|
||
public function fields(Request $request) | ||
{ | ||
return [ | ||
... | ||
|
||
Images::make(), | ||
]; | ||
} | ||
``` | ||
|
||
This will automatically use the `media` attribute on your model (which the `HasMediaTrait` adds). | ||
|
||
**Note: You currently cannot create media directly from Nova.** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"/dist/js/field.js": "/dist/js/field.js", | ||
"/dist/css/field.css": "/dist/css/field.css" | ||
"/dist/js/app.js": "/dist/js/app.js", | ||
"/dist/css/app.css": "/dist/css/app.css" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Nova.booting((Vue, router) => { | ||
Vue.component('index-nova-media-library-image-field', require('./components/IndexField')) | ||
Vue.component('detail-nova-media-library-image-field', require('./components/DetailField')) | ||
Vue.component('form-nova-media-library-image-field', require('./components/FormField')) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Kingsley\NovaMediaLibrary\Fields; | ||
|
||
use Laravel\Nova\Fields\MorphMany; | ||
|
||
class Images extends MorphMany | ||
{ | ||
/** | ||
* Create a new element. | ||
* | ||
* @return static | ||
*/ | ||
public static function make(...$arguments) | ||
{ | ||
$payload = $arguments ?: [ | ||
'Media', 'media', | ||
\Kingsley\NovaMediaLibrary\Resources\Media::class | ||
]; | ||
|
||
return new static(...$payload); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Kingsley\NovaMediaLibrary; | ||
|
||
use Laravel\Nova\Nova; | ||
use Illuminate\Support\Facades\Gate; | ||
use Laravel\Nova\Events\ServingNova; | ||
use Illuminate\Support\Facades\Route; | ||
use Spatie\MediaLibrary\Models\Media; | ||
use Illuminate\Support\ServiceProvider; | ||
use Kingsley\NovaMediaLibrary\Policies\MediaPolicy; | ||
|
||
class NovaMediaLibraryServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Bootstrap any application services. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
Nova::serving(function (ServingNova $event) { | ||
Nova::resources([ | ||
\Kingsley\NovaMediaLibrary\Resources\Media::class | ||
]); | ||
|
||
Nova::script('nova-media-library', __DIR__.'/../dist/js/app.js'); | ||
Nova::style('nova-media-library', __DIR__.'/../dist/css/app.css'); | ||
}); | ||
} | ||
|
||
/** | ||
* Register any application services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
// | ||
} | ||
} |
Oops, something went wrong.