Laravel - How to register a new user using the AuthController that Laravel provides in another controller

Codearts

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('[email protected]', 'Name Family')->to($email, $name)->subject('Confirm email');
    });

    return $user;
}
Codearts

I didn't want my OrdersController to extend my AuthController, was to lazy to extract the authentication logic into a library and just redirecting if the register checkbox was checked didn't work. I just decided to duplicate part of my registration logic inside my OrdersController. It is not good practice for sure, but it is a workaround until I extract the login into a library. Here is the code:

if (isset($_POST['register'])) {
    $this->validate($request, [
       'password' => 'required|min:6|confirmed',
       'password_confirmation' => 'required'
    ]);

    $user = User::create([
        'name' => $request['name'],
        'email' => $request['email'],
        'password' => bcrypt($request['password']),
        'phone_number' => $request['phone']
    ]); 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Register new user with laravel as an API

From Dev

Laravel creating a new user from within a controller

From Java

Manually register a user in Laravel

From Dev

How to call a controller inside another controller in laravel

From Dev

Laravel 5.1 AuthController user registration unique random id

From Dev

laravel 5.1: redirecting in AuthController doesn't work (using xampp)

From Dev

How Can I manual pass information to the register controller in Laravel

From Dev

How to get ID User from Laravel Controller

From Dev

Laravel 5.2 App\Http\Controllers\Auth\AuthController@register does not exist

From Dev

Laravel AuthController not updating function body

From Dev

new controller creation Laravel

From Dev

Unable to pass $user variable using Auth::user() in Laravel Profile controller

From Dev

How to pass a value from one controller to another controller in laravel

From Dev

How to Call a controller function in another Controller in Laravel 5

From Dev

How to pass a value from one controller to another controller in laravel

From Dev

How to use a controller inside another controller in Laravel5 as service

From Dev

how to customize registration new user laravel 5.1

From Dev

Laravel: How to redirect to subdomain using a Controller?

From Dev

Laravel using a controller in an other controller

From Dev

Laravel Auth user id in controller

From Dev

How to handle user likes another user logic in Laravel?

From Dev

Laravel 8 Register user while logged in

From Dev

Laravel - How to register custom broadcaster

From Dev

How to register service provider in laravel

From Dev

Why is overriding postRegister in Laravel AuthController not working?

From Dev

Why is overriding trait methods in Laravel AuthController not working?

From Dev

Why is overriding trait methods in Laravel AuthController not working?

From Dev

Override laravel's 5.2 authenticate method in AuthController

From Dev

Laravel - Running method of another controller in one controller

Related Related

  1. 1

    Register new user with laravel as an API

  2. 2

    Laravel creating a new user from within a controller

  3. 3

    Manually register a user in Laravel

  4. 4

    How to call a controller inside another controller in laravel

  5. 5

    Laravel 5.1 AuthController user registration unique random id

  6. 6

    laravel 5.1: redirecting in AuthController doesn't work (using xampp)

  7. 7

    How Can I manual pass information to the register controller in Laravel

  8. 8

    How to get ID User from Laravel Controller

  9. 9

    Laravel 5.2 App\Http\Controllers\Auth\AuthController@register does not exist

  10. 10

    Laravel AuthController not updating function body

  11. 11

    new controller creation Laravel

  12. 12

    Unable to pass $user variable using Auth::user() in Laravel Profile controller

  13. 13

    How to pass a value from one controller to another controller in laravel

  14. 14

    How to Call a controller function in another Controller in Laravel 5

  15. 15

    How to pass a value from one controller to another controller in laravel

  16. 16

    How to use a controller inside another controller in Laravel5 as service

  17. 17

    how to customize registration new user laravel 5.1

  18. 18

    Laravel: How to redirect to subdomain using a Controller?

  19. 19

    Laravel using a controller in an other controller

  20. 20

    Laravel Auth user id in controller

  21. 21

    How to handle user likes another user logic in Laravel?

  22. 22

    Laravel 8 Register user while logged in

  23. 23

    Laravel - How to register custom broadcaster

  24. 24

    How to register service provider in laravel

  25. 25

    Why is overriding postRegister in Laravel AuthController not working?

  26. 26

    Why is overriding trait methods in Laravel AuthController not working?

  27. 27

    Why is overriding trait methods in Laravel AuthController not working?

  28. 28

    Override laravel's 5.2 authenticate method in AuthController

  29. 29

    Laravel - Running method of another controller in one controller

HotTag

Archive