You see, I have a table Photo with these values:
Schema::create('fotos', function(Blueprint $table){
$table->increments('id');
$table->string('foto')->unique();
$table->string('nombre')->unique();
$table->date('fecha');
$table->unsignedInteger('categoria_id'); // Forarea de Categoria.
$table->foreign('categoria_id')->references('id')->on('categorias');
$table->unsignedInteger('user_id'); // Foranea de User.
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
The variable foto
is the one that stores the image, which is in public/archivo
, and I access it using this variable:
public function ruta(){
return asset("archivo/".$this->foto);
}
It turns out that I want that after deleting a photo, the image stored in the folder is also deleted. This is the function:
public function eliminar(Foto $f){
$f->limpiarImagen();
$f->delete();
return back()->with('message',['success',"La foto ha sido eliminada de la base de datos"]);
}
And this is the function that removes the image from the folder:
public function limpiarImagen(){
if(file_exists($this->ruta()))
unlink(public_path($this->ruta()));
}
But the image is not deleted. How do I solve it?