ホーム>Laravel>【Laravel】How to Fix Filesystem::has(): Argument #1 ($location) must be of type string, null given
Laravel

【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, including affiliate links, that help fund our operations.
As an Amazon Associate, Laratech earns from qualifying purchases.

Table Of Contents

  1. Error Message
  2. Solution

A summary of how to fix the Laravel error: Filesystem::has(): Argument #1 ($location) must be of type string, null given

Error Message

`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.

Please Provide Feedback
We would appreciate your feedback on this article. Feel free to leave a comment on any relevant YouTube video or reach out through the contact form. Thank you!