Do you have a foreign key with on delete?
Laravel 7 image not deleting from disk but is deleted from db
I am able to successfully add multiple images for the task (post if it were a blog) and delete the task. The task is successfully deleted from the db as well as its subsequent images. However, in the disc (by the way, I am working local at the moment) the files remain untouched. I am unsure how to handle this request as I am new to Laravel. Any help would be greatly appreciated. Here is what I have in my various files. TaskController.php showing both my store and delete functions:
public function store(Request $request)
{
$this->validate($request, [
'task_name' => 'required',
'task_description' => 'required',
]);
// Create Task
$user = Auth::user();
$task = new Task();
$data = $request->all();
$task->user_id = $user->id;
$task = $user->task()->create($data);
if ($request->hasFile('images')) {
$files = $request->file('images');
foreach ($files ?: [] as $file) {
$name = time() . '-' . $file->getClientOriginalName();
$name = str_replace(' ', '-', $name);
$file->move('task-images', $name);
$task->image()->create(['name' => $name]);
$images = new Image;
$images->name = $name;
}
}
$task->task_name = $request->input('task_name');
$task->task_description = $request->input('task_description');
$task->task_priority = $request->input('task_priority');
$task->task_assigned_by = $request->input('task_assigned_by');
$task->task_assigned_to = $request->input('task_assigned_to');
$task->task_to_be_completed_date = $request->input('task_to_be_completed_date');
$task->task_notes = $request->input('task_notes');
$task->task_status = $request->task_status;
$task->save();
return redirect('/home')->with('success', 'Task Created');
}
public function destroy($id)
{
$task = Task::findOrFail($id);
$task->delete();
return redirect('home')->with('success', 'Task Deleted');
}
Where I am attempting to delete is in my show.blade.php (just the delete form)
<form style="display: inline;" action="/tasks/{{ $task->id }}" method="POST" class="">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger btn-sm ml-1 mr-1">
<i class="fa fa-trash"></i> Delete
</button>
</form>
In my web.php, I am using a resource controller for TasksController.php.
In my Image.php Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use App\Task;
class Image extends Model
{
protected $fillable = [
'task_id',
'name',
];
protected $uploads = '/task-images/';
public function getFileAttribute($image)
{
return $this->uploads . $image;
}
public function task()
{
// return $this->belongsTo('App\Task', 'task_id');
return $this->belongsTo(Task::class);
}
public static function boot()
{
parent::boot();
self::deleting(function ($image) {
Storage::delete(Storage::path($image->name));
});
}
}
and my Task.php Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Image;
use Illuminate\Support\Facades\Storage;
class Task extends Model
{
protected $fillable = [
'task_name', 'task_priority', 'task_assigned_to', 'task_assigned_by', 'task_description', 'task_to_be_completed_date', 'task_status',
'task_notes'
];
public function user()
{
return $this->belongsTo(User::class);
}
public function image()
{
// return $this->hasMany('App\Image');
return $this->hasMany(Image::class);
}
public static function boot()
{
parent::boot();
self::deleting(function ($task) {
foreach ($task->image ?: [] as $image) {
$image->delete();
}
});
}
}
Finally, in my filesystem.php, for the disk section, I have:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
If I have missed something, please let me know and I will edit my question. Thank you in advance for your help.
Your task has multiple images. each Image is a single model and a single file
You need to get all images associated with task model, then iterate over them, deleting by name
It would help understanding if you changed the name of the relationship from image to images to reflect that there are more than one.
Please or to participate in this conversation.