Code Monkey home page Code Monkey logo

moonshine-spatie-medialibrary's Introduction

Spatie\MediaLibrary field for MoonShine Laravel admin panel

Latest Version on Packagist Total Downloads

Compatibility

MoonShine Moonshine Spatie Medialibrary Currently supported
>= v1.52 and < v2.0 <= v1.2.0 no
>= v2.0 >= v2.0.1 yes

Installation

The field is purposed for work with the Laravel-MediaLibrary package made by Spatie and extends default field Image

composer require visual-ideas/moonshine-spatie-medialibrary

Before using the Spatie\MediaLibrary field, make sure that:

  • The spatie/laravel-medialibrary package is installed and configured
  • The visual-ideas/moonshine-spatie-medialibrary package is installed
  • The field passed to Spatie\MediaLibrary is added as the name of the collection via ->addMediaCollection('Field')

In the model:

use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
 
class ModelClass extends Model implements HasMedia
{
    use InteractsWithMedia;
 
    //...
    
    public function registerMediaCollections(): void
    {
        $this->addMediaCollection('cover');
    }
    
    //...
}

In the MoonShine:

use VI\MoonShineSpatieMediaLibrary\Fields\MediaLibrary;

//...

MediaLibrary::make('Cover', 'cover'),

//...

By default, the field works in a single image mode

use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
 
class ModelClass extends Model implements HasMedia
{
    use InteractsWithMedia;
    
    //...
    
    public function registerMediaCollections(): void
    {
        $this->addMediaCollection('cover')->singleFile();
    }
    
    //...
}

If you want to use a field to load multiple images, add the ->multiple() method when declaring the field

use VI\MoonShineSpatieMediaLibrary\Fields\MediaLibrary;

//...

MediaLibrary::make('Gallery', 'gallery')->multiple(),

//...

moonshine-spatie-medialibrary's People

Contributors

alexvenga avatar dissnik avatar lee-to avatar z-takaev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

moonshine-spatie-medialibrary's Issues

Custom save media

Can you add option for customize adding media?
Or just make public/protected method addMedia
I want to prepare image before save:

class MediaLibrary extends \VI\MoonShineSpatieMediaLibrary\Fields\MediaLibrary
{
    public function addMedia(HasMedia $item, UploadedFile $file): Media
    {
        $filename = $file->getPath() . '/' . $item->slug . '.webp';

        SpatieImage::load($file->getRealPath())
            ->fit(Fit::Fill, 1920, 1080)
            ->optimize()
            ->save($filename);

        $item->addMedia($filename)->withResponsiveImages()->toMediaCollection();
    }
}

It is possible?

customName не вызывается

MediaLibrary::make('Картинки', 'images')
    ->multiple()
    ->customName(fn($item) => dd($item)) // этот коллбек не вызывается при сохранении

Поэтому, задать своё имя файла не представляется возможным.

The directory method ->dir('my_directory') doesn't work

Hi, I have tried the Moonshine admin panel with the Image class and it works very good
Image::make('Heading image', 'cover') ->disk('media') ->dir('/posts') ->allowedExtensions(['jpg', 'gif', 'png']) ->removable(),

RESULT:

/storage/public/media/posts
first_image.jpg
second_image.jpg

The MediaLibrary class also works for me, but the trouble is my directory '/posts' isn't created in my 'media' direcrory. When I add images to posts, I see new folders numerated '1' and '2' .... '10' with the images inside of them.

/storage/public/media/1
first_image.jpg
/storage/public/media/2
second_image.jpg
/storage/public/media/3...... etc.

So, the logic of method dir() of MediaLibrary class isn't the same as in Image class.

I've read the media-library.php config file also, but still don't know how to make dir() method working as it does with Image class.

В версии 2.0 при экспорте, когда поле пустое возникает ошибка

Когда экспортирую, в режиме одного файла, и этого самого файла нет возникает ошибка
VI\MoonShineSpatieMediaLibrary\Fields\MediaLibrary::resolvePreview(): Return value must be of type Illuminate\Contracts\View\View|string, null returned

Предлагаемое решение в этом самом файле строка 34 заменить на : ($this->value?->getFullUrl() ?? '');

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.