Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ErikRobles's avatar

Good Idea. I will dig deeper. Thank you for your patience. Both you and @jlrdw

newbie360's avatar

@erikrobles

in case task $id = 1 , but you are finding the image table primary id column

 public function destroy($id)
    {
        $task = Task::findOrFail($id);
        // $images = Image::find($id);

        $images = Image::find($task->id);
        
        // but look like a task has many images... find() return 1 row only

didn't see foreign key onDelete in your migration, so may be like this ?

https://github.com/alexeymezenin/laravel-best-practices

first change the method name in Task model

    public function images()
    {
        // return $this->hasMany('App\Image');
        return $this->hasMany(Image::class);
    }
public function destroy($id)
{
    $task = Task::with('images')->findOrFail($id);

    foreach($task->images as $image) {

        // i don't know how you name the image
        // check the image name in db, so make sure the path is correct
        Storage::delete('the-path-in-local-disk/' . $image->name);
    }

    // TODO: add DB transaction ?
    $task->images()->delete();
    $task->delete();

    return redirect('home')->with('success', 'Task Deleted');
}
1 like
ErikRobles's avatar

Hello @NEWBIE360. Thank you for your help. I am getting closer. When I run a dd on the storage path plus $image->name I get

"C:\laragon\www\taskapp\storage\app/public/upload/1600536086-AICerikRobles4.png"

Which is one of the file names. I am still missing something. I employed the changes you suggested. Can it be that the / and the \ in the path name are messing things up? Here is my new destroy method:

 public function destroy($id)
    {
        // $task = Task::findOrFail($id);
        $task = Task::with('images')->findOrFail($id);

        // $images = Image::find($id);

        // $images = $task->images($id)->get();

        foreach ($task->images as $image) {
            dd(storage_path('app/public/upload/' . $image->name));
            Storage::delete(storage_path('app/public/upload/' . $image->name));
        }

        $task->images()->delete();
        $task->delete();
        return redirect('home')->with('success', 'Task Deleted');
    }

I should point out that I have two or three images in this task (post if it were a blog).

jlrdw's avatar

If you comment out that dd, seems like it should work. The loop found the first image.

ErikRobles's avatar

I wish it were true. It works for the db and on the screen but not the disc. no errors. It just doesn't touch the disk.

newbie360's avatar

images table store the file name only ? without any path ?

if in database store the image file name only 1600536086-AICerikRobles4.png

and seems you created a folder taskapp\storage\app\public\ upload

so you are use public disk

Storage::disk('public')->delete('upload/' . $image->name);

actually this two are the same

local disk
Storage::delete('public/upload/' . $image->name);


public disk
Storage::disk('public')->delete('upload/' . $image->name);

in Windows taskapp\storage\app ----> is local disk

double click the public folder inside taskapp\storage\app ----> is public disk

make sense o.0?

1 like
ErikRobles's avatar

Bloody well done. Thank you so much. 1 week lost on this issue. Many Thanks to you, @snapey, @jlrdw for helping me out with this. I have learned so freaking much this week so I guess it wasn't a total loss. Thanks a million.

1 like
ErikRobles's avatar

Your answer helped set me down the path with renaming my images relationship. And @newbie360 got me the rest of the way. Thank you to both. I wish there were a way to mark 2 answers as the best.

Previous

Please or to participate in this conversation.