Peon
Peon

Reputation: 8030

Hash password before saving with Laravel Backpacker

A simple question: how do I modify (hash) the request value before saving it with Laravel Backpacker CRUD admin?

As far as i understand, it should be done somewhere before these methods are executed in the crud controller:

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

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

but I have no idea how to do it correctly.

Edit: the request is not a Request object, but rather StoreRequest or UpdateRequest that looks something like this: enter image description here

Fix:

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
}

Upvotes: 3

Views: 3286

Answers (2)

Alexey Mezenin
Alexey Mezenin

Reputation: 163898

If you're asking about how to modify data in $request variable, you can just do this:

$request->property = 'New value';

Also, you can add data to reuqest itself (not into variable):

request()->request->add(['key' => 'value']);

Upvotes: 2

Diego Vidal
Diego Vidal

Reputation: 1057

You can update $request values using the offsetSet method

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

Edit: To update user password you can do something like this:

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.');
    }
}

I did not check the code but it should work. Now update it to your needs.

Upvotes: 3

Related Questions