【Laravel】How to Fix Filesystem::has(): Argument #1 ($location) must be of type string, null given
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
I have summarized how to resolve the following error:
League\Flysystem\Filesystem::has(): Argument #1 ($location) must be of type string, null given
Solution
The error message itself was not immediately clear, but the issue was caused by the exists
method in the following code:
$isFile = Storage::disk('s3')->exists($url); // Error occurs if $url is null
This started happening after upgrading a project originally developed in Laravel 5 or 6 to Laravel 10.
Previously, when exists()
received null
, it would return false
without any issues. However, it seems that now it only accepts a string
, and null
is no longer allowed (though I am not sure of the exact change).
You can fix this by wrapping it with an if
statement like this:
$isFile = false;
if($url){
$isFile = Storage::disk('s3')->exists($url);
}
I hope this helps someone.