Question

I am working on an application with laravel which saves files in database and to a destination folder. I am trying to delete a record from the database but all I get is an error which says that the controller method is not found.

Error:

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
Controller method not found.

throw new NotFoundHttpException("Controller method not found.");

The view alluploads.blade.php has a form, where it should pass the id to the uploads/destroy function.

{{ Form::open(array('method' => 'DELETE', 'url' => array('uploads/destroy', $upload->id))) }}
     {{ Form::submit('Delete', array('class' => 'btn btn-danger')) }}
{{ Form::close() }}

And my controller has the function that deletes the row.

public function getDestroy($id)
{
     $this->upload->find($id)->delete();
     return Redirect::to('uploads/alluploads')->with('message', 'Thanks, delete was successful!');
}

The routes are have the following to access functions under uploads controller

Route::controller('uploads', 'UploadsController');
Was it helpful?

Solution

You are using HTTP method DELETE, so your controller method should be:

public function deleteDestroy($id)
{
     $this->upload->find($id)->delete();
     return Redirect::to('uploads/alluploads')->with('message', 'Thanks, delete was successful!');
}

Take a look at your routes:

php artisan routes

You must see, at least, something like this there:

+--------+---------------------------------------------------------------+----------------------+-------------------------------------------------------+--------------------------------+---------------+
| Domain | URI                                                           | Name                 | Action                                                | Before Filters                 | After Filters |
+--------+---------------------------------------------------------------+----------------------+-------------------------------------------------------+--------------------------------+---------------+
|        | DELETE uploads/destroy/{one?}/{two?}/{three?}/{four?}/{five?} |                      | UploadsController@deleteDestroy                       |                                |               |
|        | GET uploads/{_missing}                                        |                      | UploadsController@missingMethod                       |                                |               |
+--------+---------------------------------------------------------------+----------------------+-------------------------------------------------------+--------------------------------+---------------+

To use your form the way it is your form open line would have to be:

Form::open(array('method' => 'GET', 'url' => array('uploads/destroy', $upload->id)))

But I think it's better the way it is now, just rename your method.

OTHER TIPS

First try what you have with just uploads/ instead of uploads/destroy. The route name for delete is resource.destroy but the url is resource/{id} called with the delete verb

Here is how I open my form to create a delete button if $uploads is an index of uploads

@foreach($uploads as $upload)
    {{ Form::model($upload, array('url' => 'uploads/'. $upload->id, 'method' => 'delete')) }}
    {{ Form::submit('delete') }}
    {{ Form::close() }}
@endofreach
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top