SPL3S
359
11
Laravel

Can Store file but can't Destroy it

Posted 3 months ago by SPL3S

If I use just directory and file name everything is fine, I can store and destroy.

Store

public function store(Event $event, CategoryAge $categoryAge) {

        if ($validFile = request()->validate([
            'music' => 'nullable|file|mimetypes:audio/mpeg,audio/mp4,audio/mp3,audio/mpeg4-     generic|max:6000'])) {
            $music = $validFile['music'];
            $fileName = $music->getClientOriginalName();
            $musicFileName = time() . '_' . $fileName;
            $music->storeAs('public/music/', $musicFileName);

        } else {
            $musicFileName = 'no music file';
        }
}

Destroy

public function destroy(Event $event, CategoryAge $categoryAge) {
        $categoryAge->delete();

        if ($categoryAge->music != 'no music file') {
            Storage::delete('public/music/' . $categoryAge->music);
        }

But if I concatenate $event->title I can only store, for some reason I can't destroy it...

Store (which stores into nested folder that corespond to event title)

$music->storeAs('public/music/' . $event->title, $musicFileName);

Destroy doesn't work

Storage::delete('public/music/' . $event->title . "/" . $categoryAge->music);

What I might be doing wrong?

Please sign in or create an account to participate in this conversation.