Search code examples
phplaraveldestroy

RESTful Laravel controller not picking up Destroy() method


Structure: views/agents/alert/index.blade.php

Form to delete 'notification':

{{ Form::open(
    array('url'=>'agents/alert/delete/'. $alerts->id, 'role'=>'form')) }}
{{ Form::hidden('_method', 'DELETE') }}
    <button type="submit" class="btn btn-warning" id="archive">Archive</button>
{{ Form::close() }}

AgentsController:

public function destroy($id)
    {
        $alert = Alert::find($id);
        $alert->delete();

        return Redirect::to('/agents/')
        ->with('message-success', 'Your alert was successfully archived.');
    }

Routes.php: /* Agent's Route */

Route::get('agents/alert/{id}', 'AgentsController@Show');
Route::get('agents/alert/delete/{id}', 'AgentsController@Destroy');
Route::controller('agents', 'AgentsController');

I am correctly referencing the URL called when the user presses delete, however, the error presented is 'Controller Method Not Found'.

Any help I'd be thankful of.


Solution

  • You should perform a DELETE on a resource.

    If you have an alert with a unique URL agents/alert/{id} you should do a DELETE method on the same URL.

    Route::delete('agents/alert/{id}', 'AgentsController@Destroy');
    

    Create a form that can be submitted to delete the resource:

    {{ Form::open(array('method' => 'DELETE', 'action' => array('AgentsController@Destroy', $alert->id))) }}
    

    More information on RESTful controllers here.

    Also try to use named routes instead of action.