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),
]);
Related
i am trying to upload an image as a profile picture in laravel bootstrap auth package.
in this i am trying to change some package files to upload image. also i added a column in users table.
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'campus_id' => $data['campus_id'],
'role' => $data['role'],
'remarks' => $data['remarks'],
'image' => $data['image'],
]);
}
i make changes in Auth controller in validation function
also makes some changes in user store function
I think you need to move user profile image before create its entry inside database.
protected function create(array $data)
{
$imageName = time().'.'.$data['image']->extension();
//$data['image']->move(public_path('images'), $imageName);
$data['image']->storeAs('public/images', $imageName);
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'campus_id' => $data['campus_id'],
'role' => $data['role'],
'remarks' => $data['remarks'],
'image' => $imageName,
]);
}
You can use Image intervention for this. After installing you can use it in your controller as use Image;
$image = $request->file('image');
$img_name = hexdec(uniqid()).'.'.$image->getClientOriginalExtension();
Image::make($image)->resize( 847.5, 431 )->save('uploads/sliders/'.$img_name);
$image_path = 'uploads/sliders/'.$img_name;
Slider::create([
'title' => $request->title,
'image' => $image_path,
'created_at' => Carbon::now()
]);
1st you need to move your image to your desired directory inside public folder and save that directory in the database.
I have a error, I'm using sanctum and I want to check that the email does not exist
the if returns an empty array but the if is satisfied because it returns true
$mail = $request->input(['email']);
if ($search = User::where('email', $mail)->get()) {
return response()->json(['msg' => 'account already exist'], 409);
} else {
$validate = $request->validate([
'name' => 'required|string|',
'email' => 'required|string',
'password' => 'required|string'
]);
}
any solution?
Why not use the Laravel validation since this looks more like validation, so something like:
$request->validate([
'name' => 'required|string|',
'email' => 'required|string|email|unique:users,email',
'password' => 'required|string'
]);
with this you don't need to do an if else. You can check the Laravel docs on https://laravel.com/docs/8.x/validation#introduction for more details
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
I would like to use the reset password mailable for my project but I do not know how to access it in laravel 6.1
Here is my method
public function store(Request $request)
{
$validatedData = $request->validate([
'address_id' => 'required',
'name'=> 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'POBox' =>['required', 'min:6', 'max:6'],
'role_id' => 'required',
]);
$quickpass = substr( str_shuffle( str_repeat( 'abcdefghijklmnopqrstuvwxyz0123456789', 10 ) ), 0, 10 );
$newuser = User::create([
'address_id' =>$request->address_id,
'name'=> $request->name,
'email' => $request->email,
'POBox'=> $request->POBox,
'password' => Hash::make($quickpass),
'role_id' => $request->role_id,
]);
Mail::to($newuser->email)
->send( );
return view('admin.index')->with('message','The user has been created and a password reset email has been sent to them.');
}
The Notification used is Illuminate\Auth\Notifications\ResetPassword. It builds a MailMessage inline.
The PasswordBroker can be used to create the token and send off the notification for you. The method sendResetLink will take an array of credentials to find the User by.
Illuminate\Auth\Passwords\PasswordBroker
$resp = Illuminate\Support\Facades\Password::broker()->sendResetLink([
'email' => '...',
]);
I need to send an email after a new user is created.
But I don't know how to return to the home page without getting an error.
This is what I am doing right now.
User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'phone' => bcrypt($data['phone']),
'confirmation_code' => str_random(30),
]);
Email_function();
if (Auth::attempt(['email' => $data['email'], 'password' => bcrypt($data['password']) ])) {
// Authentication passed...
return redirect('/');
}
I keep getting this as my error message.
SErrorException in SessionGuard.php line 439:
Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given, called in /Applications/XAMPP/xamppfiles/htdocs/sniddl/vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php on line 63 and defined
Edit:
changed the title to reflect the answer.
Here is a modified create function with an added email function for your register controller
Make sure Request is included in the pages top with namespaces being used:
use Illuminate\Http\Request;
Change the create function in your controller:
protected function create(Request $data)
{
$user = new User;
$user->name = $data->input('name');
$user->username = $data->input('username');
$user->email = $data->input('email');
$user->password = bcrypt($data->input('password'));
$user->phone = bcrypt($data->input('phone'));
$user->confirmation_code = str_random(60);
$user->save();
if ($user->save()) {
$this->sendEmail($user);
return redirect('VIEWPATH.VIEWFILE')->with('status', 'Successfully created user.');
} else {
return redirect('VIEWPATH.VIEWFILE')->with('status', 'User not created.');
}
}
Create the sendEmail function in the same controller that will use Laravels built in email. Make sure you create and your HTML email:
public function sendEmail(User $user)
{
$data = array(
'name' => $user->name,
'code' => $user->confirmation_code,
);
\Mail::queue('EMAILVIEWPATH.HTMLEMAILVIEWFILE', $data, function($message) use ($user) {
$message->subject( 'Subject line Here' );
$message->to($user->email);
});
}
NOTE:
Your going to need to update the VIEWPATH.VIEWFILE and EMAILVIEWPATH.HTMLEMAILVIEWFILE at a minimium in the examples above.
Check the repos below for CONTROLLER :
https://github.com/laravel/laravel/blob/master/app/Http/Controllers/Auth/RegisterController.php
https://github.com/jeremykenedy/laravel-auth/blob/master/app/Http/Controllers/Auth/AuthController.php
REGISTER VIEW(blade) EXAMPLE
https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/auth/register.blade.php
EMAIL VIEW THAT RECEIVES VARIABLES:
https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/emails/activateAccount.blade.php
Ok so it turns out, by setting User::create as a variable it allows you to login in the user by returning the variable. Like this.
$user = User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'phone' => bcrypt($data['phone']),
'confirmation_code' => str_random(30),
]);
Email_function();
return $user;