3

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
}
Peon
  • 7,902
  • 7
  • 59
  • 100

2 Answers2

3

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.

Diego Vidal
  • 1,022
  • 12
  • 21
2

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']);
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • It's not a regular Laravel request, it's `StoreRequest ` or `UpdateRequest ` object, that looks different. I'll update the question shortly. – Peon Dec 13 '16 at 12:36
  • 1
    I'm sorry, I was sure I checked that option and it didn't work. Feel stupid now. Thank you. – Peon Dec 13 '16 at 12:43
  • This changes the request value, but ends up saving the original plain-text value. – Peon Dec 13 '16 at 12:47
  • Try `request()->request->add(['key' => value]);` – Alexey Mezenin Dec 13 '16 at 12:55
  • I solved it. The Backpacker CRUD by default doesn't passes the $request variable. Instead it just re-reads from the POST. I fixed it by simply adding $request in the call: `return parent::updateCrud($request)` – Peon Dec 13 '16 at 13:25