I have created a form where the admin can register accounts on the system. The problem is that the password is not hashed when the user is created on the admin side, and I think this is what stops the user from logging in on the system. Do you have any tips for me to make this work? I'm a total newbie at Laravel so.
I think the problem is in this code..
public function store(Request $request)
{
$this->validate(request(), [
'firstname' => 'required|string|max:255',
'lastname' => 'required|string|max:255',
'username' => 'required|string|max:25|unique:users',
'phone' => 'required|string|max:12|min:11|unique:users',
'address' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:8|confirmed',
'membership_term' => 'required|string|max:255',
'mode_of_payment' => 'required|string',
]);
$user = new User;
$user->firstname = $request->input('firstname');
$user->lastname = $request->input('lastname');
$user->username = $request->input('username');
$user->phone = $request->input('phone');
$user->address = $request->input('address');
$user->email = $request->input('email');
$user->password = $request->input('password');
$user->membership_term = $request->input('membership_term');
$user->mode_of_payment = $request->input('mode_of_payment');
$user->save();
return back()->with('success', 'Data Updated.')->with('users', $user);
}
Try This
use Illuminate\Support\Facades\Hash;
$user->password = Hash::make($request->input('password'));
You can use laravel helper function bcrypt()
$user->password = bcrypt($request->input('password'));
ref link https://laravel.com/docs/6.x/helpers#method-bcrypt
Related
I am trying to update the user profile. Everything works good but when i try to update name without updating the email. It gives an error saying the email already exists.
I have tried the method in the docs too but it does not work. I am using laravel 7
Here is my code
$userId = auth()->user()->id;
$validate = $request->validate([
'name' => 'required|string',
'email' => ['required', Rule::unique('users')->ignore($userId)],
'password' => 'required|',
]);
$user = User::find(auth()->user()->id);
$password = $request->password;
$user::create([
'name' => $validate['name'],
'email' => $validate['email'],
'password' => Hash::make($password),
]);
Note: I just fixed it
I was not updating the profile i was creating a new one. I was using $user::create but this code creates another row in table the which is why the validation was not working so i replaced it with this code
$user->name = $request->name;
$user->email = $request->email;
$user->password = Hash::make($password);
$user->save();
Now it works.
you can use update in place of create like this:
$user::update([
'name' => $validate['name'],
'email' => $validate['email'],
'password' => Hash::make($password),
]);
so I have this store function at my Controller
public function store(Request $request)
{
Penghuni::create([
'nama_penghuni' => $request->nama_penghuni,
'email' => $request->email,
'phone' => $request->phone,
'tower' => $request->tower,
'no_unit' => $request->no_unit
]);
User::create([
'name' => $request->nama_penghuni,
'email' => $request->email,
'password' => Hash::make($request['password']),
'role' => 'penghuni',
]);
return redirect(route('penghuni.index'));
}
what I want is make sure both insert is success, because the current result I got is when Penghuni create is done but the user is fails it keeps getting redirected
hope someone can help, I use laravel 5.8
thank you
This Code is Perfect check other things.
public function store(Request $request)
{
Penghuni::create([
'nama_penghuni' => $request->nama_penghuni,
'email' => $request->email,
'phone' => $request->phone,
'tower' => $request->tower,
'no_unit' => $request->no_unit
]);
User::create([
'name' => $request->nama_penghuni,
'email' => $request->email,
'password' => Hash::make($request['password']),
'role' => 'penghuni',
]);
return redirect(route('penghuni.index'));
}
1. Model
Penghuni and user Model must added this Line
protected $guarded = [];
Other Solution
public function store(Request $request)
{
$penghuni = new Penghuni;
$penghuni->nama_penghuni = $request->nama_penghuni;
$penghuni->email = $request->email;
$penghuni->phone = $request->phone;
$penghuni->tower = $request->tower;
$penghuni->no_unit = $request->no_unit;
$penghuni->save();
$user = new User;
$user->name = $request->nama_penghuni;
$user->email = $request->email;
$user->password = Hash::make($request->password);
$user->role = 'role';
$penghuni->users()->save($user);
return redirect(route('penghuni.index'));
}
You can wrap your queries in a database transaction like so:
DB::transaction(function () use ($request) {
// queries here
});
return redirect(route('penghuni.index'));
Or something like this, depending on your use-case.
DB::beginTransaction();
try {
// queries here
// all good
DB::commit();
return redirect(route('penghuni.index'));
} catch (\Exception $e) {
// something went wrong
DB::rollback();
}
You can read more about database transaction here: https://laravel.com/docs/8.x/database#database-transactions
I got this error
"Method Illuminate\Database\Eloquent\Collection::save does not exist."
when i'm trying to Register User API.
i dont know what 's wrong with it...
My AuthController
public function register(Request $request)
{
$request->validate([
'name' => 'required|min:3',
'email' => 'required|email',
'password' => 'required|between:6,25',
]);
$user = User::all();
$user->name = $request->name;
$user->email = $request->email;
$user->password = bcrypt($request->password);
$user->save();
return response()->json([
'registered' => true,
'token' => $user->createToken('token')->accessToken
]);
}
Any help? Thanks....
$user = User::all(); return a collection of all users in database, you should replace it with $user = new User;, which return a new User object.
Change code to
public function register(Request $request)
{
$request->validate([
'name' => 'required|min:3',
'email' => 'required|email',
'password' => 'required|between:6,25',
]);
$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = bcrypt($request->password);
$user->save();
return response()->json([
'registered' => true,
'token' => $user->createToken('token')->accessToken
]);
}
Here's my store function. I've tried playing with it a few different ways. For some reason I thought maybe putting the $post->save(); inside the if would at least change the profile image, but no. Not even that works. I initially tried return redirect()->route('profile')->withUser($user); and that didn't work either. This is my latest attempt. And still no luck so I thought to post it here. Thanks in advance! Using: Laravel 5.4
public function store(Request $request)
{
$this->validate($request, array(
'description' => 'required|string',
'projects' => 'required|string',
'experience' => 'required|string',
'links' => 'required|string',
'status' => 'required|string',
));
$userInfo = new User_Info;
$user_id = Auth()->user()->id;
$user = User::find($user_id);
$userInfo->user_id = $user_id;
$userInfo->description = $request->input('description');
$userInfo->projects = $request->input('projects');
$userInfo->experience = $request->input('experience');
$userInfo->links = $request->input('links');
$userInfo->status = $request->input('status');
$userInfo->save();
return redirect()->route('profile')->withUser($user);
}
public function settings($id)
{
$user = User::find($id);
return view("profile.settings")->withUser($user);
}
public function update(Request $request, $id)
{
$user_id = Auth()->user()->id;
$this->validate($request, array(
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'type' => 'required|string',
'description' => 'required|string',
'projects' => 'required|string',
'experience' => 'required|string',
'links' => 'required|string',
'status' => 'required|string'
));
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300, 260)->save( public_path('/images/uploads/avatars/' . $filename ) );
$user = Auth::user();
$user->avatar = $filename;
}
$user = User::find($id);
$user->first_name = $request->input('first_name');
$user->last_name = $request->input('last_name');
$user->email = $request->input('email');
$user->type = $request->input('type');
$user_info = DB::select("SELECT * FROM user_infos WHERE user_id = $user_id");
$user_info->description = $request->input('description');
$user_info->projects = $request->input('projects');
$user_info->expereince = $request->input('experience');
$user_info->status = $request->input('status');
$user->save();
$user_info->save();
return view('profile.profile')->withUser($user);
}
You don't get existing object, so change this:
$user_info = DB::select("SELECT * FROM user_infos WHERE user_id = $user_id");
To something like this:
$user_info = UserInfo::where('user_id', $user->id)->first();
In my User model I have this rule:
protected $fillable = ['name', 'email', 'password'];
And I want to add in my model one more field "role_id", that could be assigned only inside controller or model, but should be ignored in other cases.
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'role_id' => $this->role_id
]);
}
Try this:
$user = new User();
$user->fill([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password'])
]);
$user->role_id = $this->role_id;
$user->save();
return $user->id;