lukeboy_2002's avatar

error -> League\Flysystem\Filesystem::delete():

I have two almost the same controllers for update an item with uploading an image with filepond. The store function in both controllers.

One controller works and the other gives a error:

League\Flysystem\Filesystem::delete(): Argument #1 ($location) must be of type string, null given, called in /Users/antoine/Sites/TBV-TripleB/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php on line 504

Controller that works without errors

    public function edit(Sponsor $sponsor)
    {
        return view('admin.sponsors.edit', [
            'sponsor' => $sponsor,
        ]);
    }

public function update(Request $request, Sponsor $sponsor)
    {
        if (str()->afterLast($request->input('image'), '/') !== str()->afterLast($sponsor->image, '/')) {
            Storage::disk('public')->delete($sponsor->image);
            $newFilename = Str::after($request->input('image'), 'tmp/');
            Storage::disk('public')->move($request->input('image'), "sponsors/$newFilename");
        }

        $attributes = (array_merge($this->validateSponsor($sponsor), [
            'image' => isset($newFilename) ? "sponsors/$newFilename" : $sponsor->image
        ]));

        $sponsor->update($attributes);

        $request->session()->flash('success', 'Sponsor succesfully changed');

        return redirect()->route('admin.sponsors.index');
    }

protected function validateSponsor(?Sponsor $sponsor = null): array
    {
        $sponsor ??= new Sponsor();

        return request()->validate([
            'name' => [
                'required',
                'string',
                'max:255',
                Rule::unique('sponsors', 'name')->ignore($sponsor)
            ],
            'link' => [
                'required',
                'url',
                'max:255',
                Rule::unique('sponsors', 'link')->ignore($sponsor)
            ],
            'published' => [
                'nullable'
            ],
        ]);
    }

The controller that gives an error. The only difference is in the validation rules for password .

    public function edit(User $user)
    {
        return view('admin.members.edit', [
            'user' => $user,
        ]);
    }

public function update(Request $request, User $user)
    {
        if (str()->afterLast($request->input('image'), '/') !== str()->afterLast($user->profile_picture, '/')) {
            Storage::disk('public')->delete($user->profile_picture);
            $newFilename = Str::after($request->input('image'), 'tmp/');
            Storage::disk('public')->move($request->input('image'), "members/$newFilename");
        }

        $attributes = (array_merge($this->validateMember($user), [
            'profile_picture' => isset($newFilename) ? "members/$newFilename" : $user->profile_picture
        ]));

        $user->update($attributes);

        $request->session()->flash('success', 'Member succesfully changed');

        return redirect()->route('admin.members.index');
    }

protected function validateMember(?User $user = null): array
    {
        $user ??= new User();

        return request()->validate([
            'username' => [
                'required',
                'string',
                'alpha_dash',
                'max:255',
                Rule::unique('users', 'username')->ignore($user)
            ],
            'email' => [
                'required',
                'string',
                'email',
                'max:255',
                Rule::unique('users', 'email')->ignore($user)
            ],
            'password' => $this->passwordRules(),
        ]);
    }

The line that is highlighted is:

Storage::disk('public')->delete($user->profile_picture);

Please help, because I don't get it.

0 likes
1 reply
LaryAI's avatar
Level 58

The error message indicates that the argument passed to the delete method of the Storage facade is null instead of a string. This can happen if the $user->profile_picture property is null or empty.

To fix this, you can add a check to make sure that the property is not null before calling the delete method. You can use the optional helper function to do this in a concise way:

if (optional($user->profile_picture)->isNotEmpty()) {
    Storage::disk('public')->delete($user->profile_picture);
}

This code checks if $user->profile_picture is not null and not an empty string before calling the delete method. If it is null or empty, the method call is skipped and no error is thrown.

You can replace the existing line Storage::disk('public')->delete($user->profile_picture); with this code in the update method of the MembersController and it should fix the error.

Please or to participate in this conversation.