dmitrybubyakin / nova-medialibrary-field

Laravel Nova field for managing the Spatie media library
MIT License
262 stars 62 forks source link

Problem with UUID primary keys for models #161

Closed jacob418 closed 2 years ago

jacob418 commented 2 years ago

In my project I'm using UUID for the primary keys of models, therefore I had to change the media table to use uuidMorphs instead of normal morphs. \DmitryBubyakin\NovaMedialibraryField\Http\Requests\MedialibraryRequest::resourceExists checks weather a model exists by testing weather the param resourceId is numeric. Since UUID's are not numeric \DmitryBubyakin\NovaMedialibraryField\TransientModel comes into use. However this model has a numeric key, so it will cause an exception [...] invalid input syntax for type uuid: "1" (SQL: select * from "media" where "media"."model_id" = 1 and "media"."model_id" is not null and "media"."model_type" = DmitryBubyakin\NovaMedialibraryField\TransientModel).

Is there any way to get this package to work with UUID's?

dmitrybubyakin commented 2 years ago

@jacob418 UUID as PK is not supported

jacob418 commented 2 years ago

That's sad because this way we can not use this package at all, I was really looking forward to use it.

zoltiecodes commented 1 year ago

Hi.

You can easily solve this issue by replacing the MedialibraryRequest class in the container.

Create a request class

php artisan make:request MediaLibraryRequest

Extend the original class and overwrite the resourceExists method

<?php

namespace App\Http\Requests;

class MediaLibraryRequest extends \DmitryBubyakin\NovaMedialibraryField\Http\Requests\MedialibraryRequest
{
    public function resourceExists(): bool
    {
        return $this->route('resourceId') !== 'undefined';
    }
}

Replace the class in the container in your AppServiceProvider

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(MedialibraryRequest::class, \App\Http\Requests\MediaLibraryRequest::class);
    }
}
jacob418 commented 1 year ago

@zsoltgyure ty for the tip, I hope i get to try it out some day.