Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
Change a lot of things
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslkingsley committed Sep 5, 2018
1 parent 5f39295 commit f89b6c9
Show file tree
Hide file tree
Showing 16 changed files with 22,877 additions and 71 deletions.
42 changes: 22 additions & 20 deletions README.md
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.**
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jameslkingsley/nova-media-library-field",
"description": "A Laravel Nova field for displaying and updating a Spatie Media Library model.",
"name": "jameslkingsley/nova-media-library",
"description": "Laravel Nova tools for managing the Spatie media library.",
"keywords": [
"laravel",
"nova"
Expand All @@ -11,13 +11,13 @@
},
"autoload": {
"psr-4": {
"Kingsley\\MediaLibraryField\\": "src/"
"Kingsley\\NovaMediaLibrary\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Kingsley\\MediaLibraryField\\MediaLibraryFieldServiceProvider"
"Kingsley\\NovaMediaLibrary\\NovaMediaLibraryServiceProvider"
]
}
},
Expand Down
Empty file added dist/css/app.css
Empty file.
1 change: 1 addition & 0 deletions dist/js/app.js

Large diffs are not rendered by default.

22,606 changes: 22,605 additions & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions mix-manifest.json
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"
}
5 changes: 5 additions & 0 deletions resources/js/app.js
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'))
})
2 changes: 1 addition & 1 deletion resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default {
data: () => ({
file: null,
label: 'no file selected',
label: 'No file selected',
fileName: '',
removeModalOpen: false,
missing: false,
Expand Down
5 changes: 0 additions & 5 deletions resources/js/field.js

This file was deleted.

File renamed without changes.
23 changes: 20 additions & 3 deletions src/MediaLibraryField.php → src/Fields/Image.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
<?php

namespace Kingsley\MediaLibraryField;
namespace Kingsley\NovaMediaLibrary\Fields;

use Illuminate\Http\Request;
use Laravel\Nova\Fields\Field;
use Laravel\Nova\Fields\Deletable;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Contracts\Deletable as DeletableContract;

class MediaLibraryField extends Field
class Image extends Field implements DeletableContract
{
use Deletable;

/**
* The field's component.
*
* @var string
*/
public $component = 'media-library-field';
public $component = 'nova-media-library-image-field';

/**
* Hydrate the given attribute on the model based on the incoming request.
Expand Down Expand Up @@ -112,4 +117,16 @@ public function __call($method, $arguments)
{
return $this->withMeta(['ml_' . $method => $arguments]);
}

/**
* Get additional meta information to merge with the element payload.
*
* @return array
*/
public function meta()
{
return array_merge([
'deletable' => isset($this->deleteCallback) && $this->deletable
], $this->meta);
}
}
23 changes: 23 additions & 0 deletions src/Fields/Images.php
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);
}
}
33 changes: 0 additions & 33 deletions src/MediaLibraryFieldServiceProvider.php

This file was deleted.

41 changes: 41 additions & 0 deletions src/NovaMediaLibraryServiceProvider.php
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()
{
//
}
}
Loading

0 comments on commit f89b6c9

Please sign in to comment.