dmitrybubyakin / nova-medialibrary-field

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

Don't works upload file from create form but work in edit form #168

Open 4n70w4 opened 1 year ago

4n70w4 commented 1 year ago

Hi!

dmitrybubyakin/nova-medialibrary-field         4.0.1
laravel/nova                                   4.15.2
Medialibrary::make('name', 'avatar'),

If I just open create form I get error:

https://od.localhost/nova-vendor/dmitrybubyakin/nova-medialibrary-field/categories/undefined/media/name?fieldUuid=76127e81-b1bc-4299-b1f2-6caf8c6e6b11
SQLSTATE[22P02]: Invalid text representation: 7 ERROR:  invalid input syntax for type smallint: \"undefined\"\nCONTEXT:  unnamed portal parameter $1 = '...' (SQL: select * from \"categories\" where \"categories\".\"id\" = undefined limit 1)

If I try to upload file by created form I also get the same error.

https://od.localhost/nova-vendor/dmitrybubyakin/nova-medialibrary-field/categories/undefined/media/name
SQLSTATE[22P02]: Invalid text representation: 7 ERROR:  invalid input syntax for type smallint: \"undefined\"\nCONTEXT:  unnamed portal parameter $1 = '...' (SQL: select * from \"categories\" where \"categories\".\"id\" = undefined limit 1)

Generated url's contain undefined as resourceId

4n70w4 commented 1 year ago

Also I get the same error if I try to upload file by edit form (but only in first time):

https://od.localhost/nova-vendor/dmitrybubyakin/nova-medialibrary-field/categories/undefined/media/avatar?fieldUuid=7c0fca01-3c64-4adb-9338-24b6ce79a1f0
SQLSTATE[22P02]: Invalid text representation: 7 ERROR:  invalid input syntax for type smallint: \"undefined\"\nCONTEXT:  unnamed portal parameter $1 = '...' (SQL: select * from \"categories\" where \"categories\".\"id\" = undefined limit 1)

File uploaded successfully, but display error.

No errors if I again try to upload file by edit form (without submit and without refresh page).

uzdeveloper1 commented 1 year ago

public function handle(Request $request, Closure $next) {

    if ($request->route()->hasParameter('resourceId') && $request->resourceId === 'undefined') {

       $request->route()->setParameter('resourceId', null);
    }
    return $next($request);
}
uzdeveloper1 commented 1 year ago

public function handle(Request $request, Closure $next) {

    if ($request->route()->hasParameter('resourceId') && $request->resourceId === 'undefined') {

       $request->route()->setParameter('resourceId', null);
    }
    return $next($request);
}

Add this middleware