So I am building a checkout functionality. If the user is a guest he could use his credentials to make a new registration with the order he makes. I have used the auth scaffold Laravel provides with the command php artisan make:auth
. I have an OrdersController and the AuthController that comes with the framework. How can I register a new user within my OrdersController? Here is my code:
checkout.blade.php:
<li class="col-md-12 col-sm-12">
<h4>Register</h4>
<input id="create-act" type="checkbox" class="input-chkbox" name="register">
<label for="create-act">Create user account?</label>
</li>
<li class="col-md-12 col-sm-12 create-account">
<label for="password">Password<em>*</em></label>
<input required type="text" class="input-text" id="password"
name="password">
</li>
<li class="col-md-12 col-sm-12 create-account">
<label for="password_confirmation">Confirm Password<em>*</em></label>
<input required type="text" class="input-text"
id="password_confirmation"
name="password_confirmation">
</li>
OrdersController inside addOrder method:
if(isset($_POST['register'])) {
$this->validate($request, [
'password' => 'required|password',
'confirm_password' => 'required|'
]);
}
I want to register the new user inside this if. How can I do that?
Edit: Here is my protected function create inside my AuthController.
protected function create(array $data)
{
$confirmationCode = str_random(30);
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'confirmation_code' => $confirmationCode
]);
$user->roles()->attach(Role::where('name', 'Customer')->first());
$cart = new Cart();
$cart->user_id = $user->id;
$cart->save();
if(Session::has('cartItems')) {
foreach (Session::get('cartItems') as $sessionCartItem) {
$cartItem = new CartItem();
$cartItem->product_id = $sessionCartItem['product']->id;
$cartItem->cart_id = $cart->id;
$cartItem->size = $sessionCartItem['size'];
$cartItem->quantity = $sessionCartItem['quantity'];
$cartItem->save();
}
}
Session::flush();
$cart->save();
$email = $user['email'];
$name = $user['name'];
Mail::send('auth.emails.user-confirmation', ['name' => $name, 'confirmation_code' => $user['confirmation_code']], function ($message) use ($email, $name) {
$message->from('mymail@gmail.com', 'Name Family')->to($email, $name)->subject('Confirm email');
});
return $user;
}