More efficient Way - delete files and rows

Posted 3 years ago by kshorne

Hi guys, i was searching recently for a way to delete files when i triggered a cascade of my main table but to no avail, so ive hacked together this. My question is, is there a better way you can think of doing this? It seems very explicit which is in contrast to what laravel seems to aim for (implicit)

        if ($request->confirm === 'true') {
            $section = Section::findOrFail($request->id);
                foreach ($section->download as $download) {
                    $delete_download = Download::findOrFail($download->id);
                    \File::delete($delete_download->path);
                    $delete_download->delete();
                }
                foreach ($section->suppimg as $suppimg) {
                    $delete_suppimg = Suppimg::findOrFail($suppimg->id);
                    \File::delete($delete_suppimg->path);
                    $delete_suppimg->delete();
                }
            $section->delete();
            flash('success', 'Section Deleted', 'Page section, images and downloads successfully deleted');         
        } else {
            flash('error', 'Error', 'You must confirm deletion of section');
            return back();
        }
    } ```

                

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

Reply to

Use Markdown with GitHub-flavored code blocks.