0

Hi I am trying to soft delete and restore a user using a form, I am using a couple of packages for user auth and roles which are Zizaco Confide and Zizaco Entrust. I've added the following to the user.php model

use SoftDeletingTrait;
use ConfideUser;
use HasRole;
protected $softDelete = true;

and I've run a test as so to test this works:

Route::get('/deleteme', function(){ 
    User::find(2)->delete();
    return 'done';
});

and this updated the timestamp field, however I want to put this into my controller to neaten things up and give it a form. So I've done this in the table of users:

 @if(empty($user->deleted_at))
           {{Form::open(['method'=>'PATCH','action'=>
            ['UsersController@softDeleteUser',$user->id]])}}
              <button type="submit">Suspend</button>
              {{Form::close()}}  
            @else 
             {{Form::open(['method'=>'delete','action'=>
            ['UsersController@restoreUser',$user->id]])}}
                <button type="submit">Re-activate</button>
            {{Form::close()}}  
            @endif

and in my Controller:

public function softDeleteUser($id){
    $user = User::find($id);
    $user->delete();
    // redirect
    return Redirect::to('/admin');
}

public function restoreUser($id) {
    User::find($id)->restore();        
    $user->save();
    Redirect::to("/admin");
}

In my routes:

Route::post('/admin/user/{resource}/delete', 
       array('as' => 'admin.user.delete', 'uses' 
       =>'UsersController@softDeleteUser'));
Route::post('/admin/user/{resource}/restore', 
      array('as' => 'admin.user.restore', 
     'uses' =>'UsersController@restoreUser'));

However I get this error:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException 

Any ideas what I'm doing wrong??

1 Answer 1

1

Well you've set your two forms to use the PATCH and DELETE method but your routes are set to POST (Route::post).

You can either change the routes:

Route::patch('/admin/user/{resource}/delete', 
    array('as' => 'admin.user.delete', 'uses' 
    =>'UsersController@softDeleteUser'));
Route::delete('/admin/user/{resource}/restore', 
    array('as' => 'admin.user.restore', 
   'uses' =>'UsersController@restoreUser'));

Or remove the method in your forms (it will default to POST)

{{Form::open(['action'=> ['UsersController@softDeleteUser',$user->id]])}}
     <button type="submit">Suspend</button>
{{Form::close()}}

And

{{Form::open(['action'=> ['UsersController@restoreUser',$user->id]])}}
     <button type="submit">Re-activate</button>
{{Form::close()}}  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.