duannuochi3549 2014-04-26 03:00
浏览 28
已采纳

Laravel强制删除会生成NotFoundHttpException

I have read as many posts as possible, but none of them can solve my problem.

The route:

Route::model('user', 'User');
Route::group(array('prefix' => 'admin'), function() {
    Route::get('users/force-delete/{user}', array(
        'as'    => 'admin-users-force-delete',
        'uses'  => 'AdminController@handleUserForceDelete'
    ));
});

The html:

<li><a href="{{ action('AdminController@handleUserForceDelete', $user->id) }}">Force Delete</a></li>

The handler:

public function handleUserForceDelete(User $user)
    {


        $username_tmp = $user->username;
        $message = 'Success! User ' . $username_tmp . ' has been deleted.';

        if($user->trashed())
        {
            $user->forceDelete();
            return Redirect::action('AdminController@showUsers')->with('message', $message);
        } else {
            return Redirect::action('AdminController@showUsers')->with('message', 'User deletion error! Please try again!');
        }
    }

I tried to put delete and force-delete at the same handler, and the delete action took place but force-delete generated NotFoundHttpException. So I guess the problem is from the force-delete action??

  • 写回答

1条回答 默认 最新

  • duanjie9630 2014-04-26 03:31
    关注

    I solved it!
    For anyone with the same trouble, soft deleted user(or anything) will not generate an instance passed to the handler (or closure). Therefore, for this case I manually create an instance.

    So instead of using this:

    //will not handle soft deleted model.
    Route::model('user', 'User');
    

    Use this:

    Route::bind('user', function($value, $route)
    {
        return User::withTrashed()->where('id', '=', $value)->first();
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?