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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀