OunknownO OunknownO - 7 days ago 4
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::findOrFail($id)->delete();
$file_path = storage_path('documents').'/'.$file_name;
$destinationPath = $file_path; File::delete($file_path);
return redirect('/documents');
}


This is the route

Route::delete('documents/{file}/{id}','FilesController@destroyFile');


And when i press submit button I get NotFoundHttpException

Answer

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">

or

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