Laravel Nova + Spatie媒体库

时间:2018-08-26 16:56:50

标签: laravel laravel-5 spatie laravel-nova

尝试在Spatie Media Library中使用Laravel Nova。我创建了这样的上传字段:

Image::make('Logo')
        ->store(function (Request $request, $model) {
            $model->addMediaFromRequest('logo')->toMediaCollection('manufacturers');
        }),

接缝正常,但Nova仍尝试将文件名保存到厂商表中的“徽标”列。

自定义该字段的原始示例为:

File::make('Attachment')
    ->store(function (Request $request, $model) {
        return [
            'attachment' => $request->attachment->store('/', 's3'),
            'attachment_name' => $request->attachment->getClientOriginalName(),
            'attachment_size' => $request->attachment->getSize(),
        ];
    })

3 个答案:

答案 0 :(得分:1)

我通过在模型上设置一个空的mutator找到了解决方法。在您的情况下,将是:

class Manufacturer extends Model implements HasMedia
{
    use HasMediaTrait;

    public function setLogoAttribute() {}

    //...
}

这是我整个实现的一个例子。请注意,当前在Nova 1.0.6中,preview()方法不起作用,它返回了thumbnail()网址。

App / GalleryItem

class GalleryItem extends Model implements HasMedia
{
    use HasMediaTrait;

    public function setImageAttribute() {}

    public function registerMediaConversions(Media $media = null)
    {
        $this->addMediaConversion('thumbnail')
            ->fit(Manipulations::FIT_CROP, 64, 64);

        $this->addMediaConversion('preview')
            ->fit(Manipulations::FIT_CROP, 636, 424);

        $this->addMediaConversion('large')
            ->fit(Manipulations::FIT_CONTAIN, 1920, 1080)
            ->withResponsiveImages();
    }

    public function registerMediaCollections()
    {
        $this->addMediaCollection('images')->singleFile();
    }
}

App / Nova / GalleryItem

class GalleryItem extends Resource
{
    public static $model = 'App\GalleryItem';

    public static $with = ['media'];

    public function fields(Request $request)
    {
        return [
            Image::make('Image')
                ->store(function (Request $request, $model) {
                    $model->addMediaFromRequest('image')->toMediaCollection('images');
                })
                ->preview(function () {
                    return $this->getFirstMediaUrl('images', 'preview');
                })
                ->thumbnail(function () {
                    return $this->getFirstMediaUrl('images', 'thumbnail');
                })
                ->deletable(false);
        ];
    }
}

答案 1 :(得分:1)

    Image::make('Logo')
        ->store(function (Request $request, $model) {
            $model->addMediaFromRequest('logo')->toMediaCollection('manufacturers');
        return [];
        }),

也许返回一个空数组会阻止nova保存名称。

答案 2 :(得分:0)

Nova允许您从回调中返回true,以表明处理已完成,并且它本身不应设置任何属性。

这是运行回调的代码:

protected function fillAttribute(NovaRequest $request, $requestAttribute, $model, $attribute)
{
    //...

    $result = call_user_func($this->storageCallback, $request, $model);

    if ($result === true) {
        return;
    }

    if (! is_array($result)) {
        return $model->{$attribute} = $result;
    }

    foreach ($result as $key => $value) {
        $model->{$key} = $value;
    }
}

因此,true或任何空数组都可以实现相同的效果,但是从个人角度看,做前者会更清楚。