Pieterjan Neskens
Pieterjan Neskens

Reputation: 31

Laravel remove files from storage

I'm trying to remove all documents from a customer:

        // remove extra documents
        $documents = Document::where('customer_id', $customer->id);
        foreach ($documents as $document) {
            Storage::disk('userfiles')->delete($document->name);
        }
        $documents->delete();

Records get deleted from database, but files stay in storage folder. Filename in database is the same as filename in storage folder. (checked)

I also use the following code in the same method that does work:

        Storage::disk('userfiles')->delete($customer->id_card);

I'm using Angular as front-end and I'm new to both.

Upvotes: 1

Views: 3830

Answers (1)

Pieterjan Neskens
Pieterjan Neskens

Reputation: 31

So I found a solution,

            // remove extra documents
            $documents = Document::where('customer_id', $customer->id)->get();
            foreach ($documents as $document) {
                Storage::disk('userfiles')->delete($document->name);
                $document->delete();
            }

Without ->get() the $documents variable is empty, so it doens't loop. With ->get() the $documents->delete() doesn't work, so I moved it inside the loop and delete each record on its own with $document->delete()

Upvotes: 2

Related Questions