Reputation: 147
This is the method inside my API controller which is suppose to update the User data. One of the columns is password which is suppose to be hashed before insert.
public function update($id, Request $request)
{
$user = $this->userRepository->findWithoutFail($id);
if (empty($user)) {
return $this->sendResponse([
'error' => true,
'code' => 404,
], 'User not found');
}
$input = $request->except(['api_token']);
try {
if ($request->has('device_token')) {
$user = $this->userRepository->update($request->only('device_token'), $id);
} else {
$customFields = $this->customFieldRepository->findByField('custom_field_model', $this->userRepository->model());
$user = $this->userRepository->update($input, $id);
foreach (getCustomFieldsValues($customFields, $request) as $value) {
$user->customFieldsValues()
->updateOrCreate(['custom_field_id' => $value['custom_field_id']], $value);
}
}
} catch (ValidatorException $e) {
return $this->sendError($e->getMessage(), 401);
}
return $this->sendResponse($user, __('lang.updated_successfully', ['operator' => __('lang.user')]));
}
Right now, upon updating the User data using this method, the raw text of the password is inserted to the User table. I want it to be hashed using the following code. But i do not know where should this go in the method above. Can someone please help ??
$user->password = Hash::make($request->input('password'));
Example of the body of request which would come to update :
{"id":"147","name":"tipumon","email":"[email protected]","phone":"9898365627","useraddress":"test address","password":"passwordtext","api_token":"<apiTokenstring>","bio":"","media":{"id":null,"name":null,"url":"https://example.com/public/images/image_default.png","thumb":"https://example.com/public/images/image_default.png","icon":"https://example.com/public/images/image_default.png","formated_size":null}}
Upvotes: 0
Views: 238
Reputation: 147
Solved myself.
public function update($id, Request $request)
{
$user = $this->userRepository->findWithoutFail($id);
if (empty($user)) {
return $this->sendResponse([
'error' => true,
'code' => 404,
], 'User not found');
}
$input = $request->except(['api_token']);
$input['password'] = Hash::make($request->input('password'));
Upvotes: 1