Getting an error in Laravel 6.2
Symfony\Component\Debug\Exception\FatalThrowableError
Return value of Spatie\MediaLibrary\PathGenerator\BasePathGenerator::getBasePath() must be of the type string, null returned
vendor/spatie/laravel-medialibrary/src/PathGenerator/BasePathGenerator.php:39
The tests prove that the latest version of this package is working perfectly. Could you try installing the latest version of the pacakge in a vanilla laravel app and check if you are having problems there?
I have the same error in Lumen 6.2.0
Return value of Spatie\MediaLibrary\PathGenerator\BasePathGenerator::getBasePath() must be of the type string, null returned in .... /vendor/spatie/laravel-medialibrary/src/PathGenerator/BasePathGenerator.php:39
I got same error when i tried to use custom model
e.g New Model
checking this issue made me try the normal model and it worked https://github.com/spatie/laravel-medialibrary/issues/1036#issue-313783509
I dont know if its the same in your case.
I had this error and it occurred because I didn't save the model first $model->save() before creating media to attach and calling $media->getUrl()
So check to make sure you save the model first and it exists in the database if you are calling that method.
I'm having the same error in Laravel 7.0 while uploading image
The data gets uploaded in the database but image isn't saved and I get this error
Symfony\Component\Debug\Exception\FatalThrowableError
Return value of Spatie\MediaLibrary\PathGenerator\BasePathGenerator::getBasePath() must be of the type string, null returned
Same here, it happends when using a CustomMedia model and you save the parent model.
I seem to be having this issue when casting the result of toMediaCollection() to a string. For example, in a controller, if I do:
return $model->addMedia($file)
->toMediaCollectionOnCloudDisk($media_collection);
That error crops up.
Improvements on this were made in the latest version of the medialib. If possible, upgrade to the latest version.
Is there still error. I got similar issue where getBasePath returns null .
Spatie\MediaLibrary\MediaCollections\Models\Media {#1593
#table: "media"
#guarded: []
#casts: array:3 [
"manipulations" => "array"
"custom_properties" => "array"
"responsive_images" => "array"
]
#connection: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: array:10 [
"name" => "newPhoto"
"file_name" => "newPhoto.jpg"
"disk" => "media"
"conversions_disk" => "media"
"collection_name" => "51620baf-7e05-4e72-b9a3-176a2cc419d0"
"mime_type" => "image/jpeg"
"size" => 29425
"custom_properties" => "[]"
"responsive_images" => "[]"
"manipulations" => "[]"
]
#original: []
#changes: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}
Here is the error resp
{
"message": "Return value of Spatie\\MediaLibrary\\Support\\PathGenerator\\DefaultPathGenerator::getBasePath() must be of the type string, null returned",
"exception": "TypeError",
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/spatie/laravel-medialibrary/src/Support/PathGenerator/DefaultPathGenerator.php",
"line": 38,
"trace": [
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/spatie/laravel-medialibrary/src/Support/PathGenerator/DefaultPathGenerator.php",
"line": 14,
"function": "getBasePath",
"class": "Spatie\\MediaLibrary\\Support\\PathGenerator\\DefaultPathGenerator",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/spatie/laravel-medialibrary/src/Support/UrlGenerator/BaseUrlGenerator.php",
"line": 51,
"function": "getPath",
"class": "Spatie\\MediaLibrary\\Support\\PathGenerator\\DefaultPathGenerator",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/spatie/laravel-medialibrary/src/Support/UrlGenerator/DefaultUrlGenerator.php",
"line": 12,
"function": "getPathRelativeToRoot",
"class": "Spatie\\MediaLibrary\\Support\\UrlGenerator\\BaseUrlGenerator",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/spatie/laravel-medialibrary/src/MediaCollections/Models/Media.php",
"line": 64,
"function": "getUrl",
"class": "Spatie\\MediaLibrary\\Support\\UrlGenerator\\DefaultUrlGenerator",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/app/Nova/Patches/PTrix.php",
"line": 70,
"function": "getUrl",
"class": "Spatie\\MediaLibrary\\MediaCollections\\Models\\Media",
"type": "->"
},
{
"function": "App\\Nova\\Patches\\{closure}",
"class": "App\\Nova\\Patches\\PTrix",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/nova/src/Http/Controllers/TrixAttachmentController.php",
"line": 25,
"function": "call_user_func"
},
{
"function": "store",
"class": "Laravel\\Nova\\Http\\Controllers\\TrixAttachmentController",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Routing/Controller.php",
"line": 54,
"function": "call_user_func_array"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php",
"line": 45,
"function": "callAction",
"class": "Illuminate\\Routing\\Controller",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
"line": 239,
"function": "dispatch",
"class": "Illuminate\\Routing\\ControllerDispatcher",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
"line": 196,
"function": "runController",
"class": "Illuminate\\Routing\\Route",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 685,
"function": "run",
"class": "Illuminate\\Routing\\Route",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 128,
"function": "Illuminate\\Routing\\{closure}",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/nova/src/Http/Middleware/Authorize.php",
"line": 18,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Laravel\\Nova\\Http\\Middleware\\Authorize",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/nova/src/Http/Middleware/BootTools.php",
"line": 20,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Laravel\\Nova\\Http\\Middleware\\BootTools",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/nova/src/Http/Middleware/DispatchServingNovaEvent.php",
"line": 20,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Laravel\\Nova\\Http\\Middleware\\DispatchServingNovaEvent",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php",
"line": 41,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Routing\\Middleware\\SubstituteBindings",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php",
"line": 44,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/nova/src/Http/Middleware/Authenticate.php",
"line": 31,
"function": "handle",
"class": "Illuminate\\Auth\\Middleware\\Authenticate",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Laravel\\Nova\\Http\\Middleware\\Authenticate",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php",
"line": 76,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php",
"line": 49,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\View\\Middleware\\ShareErrorsFromSession",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php",
"line": 116,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php",
"line": 62,
"function": "handleStatefulRequest",
"class": "Illuminate\\Session\\Middleware\\StartSession",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Session\\Middleware\\StartSession",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php",
"line": 37,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php",
"line": 66,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Cookie\\Middleware\\EncryptCookies",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 103,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 687,
"function": "then",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 662,
"function": "runRouteWithinStack",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 628,
"function": "runRoute",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 617,
"function": "dispatchToRoute",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
"line": 165,
"function": "dispatch",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 128,
"function": "Illuminate\\Foundation\\Http\\{closure}",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/kabbouchi/nova-impersonate/src/Http/Middleware/Impersonate.php",
"line": 23,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "KABBOUCHI\\NovaImpersonate\\Http\\Middleware\\Impersonate",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/nova/src/Http/Middleware/ServeNova.php",
"line": 26,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Laravel\\Nova\\Http\\Middleware\\ServeNova",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
"line": 21,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
"line": 21,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php",
"line": 27,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php",
"line": 63,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/fruitcake/laravel-cors/src/HandleCors.php",
"line": 37,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Fruitcake\\Cors\\HandleCors",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/fideloper/proxy/src/TrustProxies.php",
"line": 57,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Fideloper\\Proxy\\TrustProxies",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 103,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
"line": 140,
"function": "then",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
"line": 109,
"function": "sendRequestThroughRouter",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->"
},
{
"file": "/Users/quantum/Desktop/code/dumb_app/public/index.php",
"line": 55,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->"
},
{
"file": "/Users/quantum/.composer/vendor/laravel/valet/server.php",
"line": 191,
"function": "require"
}
]
}
Most helpful comment
I had this error and it occurred because I didn't save the model first
$model->save()before creating media to attach and calling$media->getUrl()So check to make sure you save the model first and it exists in the database if you are calling that method.