Good Idea. I will dig deeper. Thank you for your patience. Both you and @jlrdw
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');
}
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).
If you comment out that dd, seems like it should work. The loop found the first image.
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.
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?
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.
Please or to participate in this conversation.