php - 在使用 Laravel Backpacker 保存之前哈希密码

标签 php laravel crud laravel-backpack

一个简单的问题:在使用 Laravel Backpacker CRUD admin 保存请求值之前如何修改(散列)请求值?

据我所知,它应该在 crud Controller 中执行这些方法之前的某处完成:

public function store(StoreRequest $request)
{
    return parent::storeCrud();
}

public function update(UpdateRequest $request)
{
    return parent::updateCrud();
}

但我不知道如何正确地做到这一点。

编辑:请求不是 Request 对象,而是 StoreRequestUpdateRequest 看起来像这个: enter image description here

修复:

public function update(UpdateRequest $request)
{
    // Hash password before save
    if (!empty($request->password)) {
        $request->offsetSet('password', Hash::make($request->password));
    }

    return parent::updateCrud($request); // <-- Pass the modified request, otherwise the CRUD reads it again from post data
}

最佳答案

您可以使用 offsetSet 更新 $request 值方法

$request->offsetSet('name', $newName);

编辑:要更新用户密码,您可以这样做:

public function update_password(Request $request)
{
    $user = User::find(Auth::user()->id);

    if (Hash::check($request->old_password, $user->password)) {
        $user->fill([
            'password' => Hash::make($request->password)
        ])->update();

        return redirect()->back()->with('message' => 'Your password has been updated.');
    }
    else {
        return redirect()->back()->with('message' => 'The password entered do not match our records.');
    }
}

我没有检查代码,但它应该可以工作。现在根据您的需要对其进行更新。

关于php - 在使用 Laravel Backpacker 保存之前哈希密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41121167/

相关文章:

php - MYSQL 跨表更新不工作

javascript - 如何在sugarcrm中制作弹出窗口?

php - 拉维尔 5.6 : Create schema in database

php - 如何在 Yii 中为单个日期属性设置多个字段(D/M/Y)?

javascript - javascript onclick 属性如何通过循环从数组中获得更多输出?

php - 获取加入数组的结果

php - 使用 AJAX 逐渐显示数据库中的大量数据 - LARAVEL

Laravel required_unless 验证

node.js - MEAN Stack CRUD todolist 给出无限列表,不会删除

entity-framework - 如何在 FluentAPI 映射表中插入数据