OunknownO OunknownO - 10 months ago 54
PHP Question

Laravel deleting a file NotFoundHttpException

this is my form in my view

{!! Form::open(['url' => ['documents/{file}/{id}', $file->name, $file->id],'method' => 'delete']) !!}
{!! Form::token() !!}
{!! Form::submit('Delete') !!}
{!! Form::close() !!}

controller in which i delete file from database and the original file

public function destroyFile($file_name, $id)
$file_path = storage_path('documents').'/'.$file_name;
$destinationPath = $file_path; File::delete($file_path);
return redirect('/documents');

This is the route


And when i press submit button I get NotFoundHttpException

Answer Source

Try to use this

{!! Form::open(['method' => 'DELETE', 'action' => ['FilesController@destroyFile', $file->name, $file->id] ]) !!}

Actually, their answers are correct. You need the _method to be DELETE. When I am using this. Laravel do it for me.

Or you can put this on your form

<input type="hidden" name="_method" value="DELETE">


{!! Form::hidden('_method', 'DELETE') !!}