By ViktorasssIT


2020-02-14 08:19:39 8 Comments

I made a register form in the front_page and I want to redirect the user after a successfull registration to /home page, but instead I get an array and the user is not redirected.

  {"email":"[email protected]","paypal_email":"[email protected]","updated_at":"2020-02- 
  14 
  08:16:32","created_at":"2020-02-14 08:16:32","id":1}

My route:

 Route::post('/', 'Auth\[email protected]')->name('auth.register');

Register controller:

      public function create(Request $request)
{
    return User::create([
        'email' => $request['email'],
        'paypal_email' => $request['paypal_email'],
        'password' => Hash::make($request['password']),
    ]);


}    

  public $redirectTo = '/home';

2 comments

@Andy Song 2020-02-14 08:31:55

You should not call the create method directly, for a few reasons.

  1. the request is not being validated.
  2. if you want to trigger some events during registration that is not going to happen.
  3. it's not going auto-login the user.

The create method is really for laravel trait RegistersUsers internal usage. you can keep the controller like that, you only need to change your route to register.

Route::post('/', 'Auth\[email protected]')->name('auth.register');

By doing this the user should be created and redirected to home page.

And there is a reason why public $redirectTo = '/home'; this attribute exists, by using the create method directly you are ignoring the attribute completely.

@Ebrahim Mohammed 2020-02-14 08:24:36

Change this:

public function create(Request $request)
{
    return User::create([
        'email' => $request['email'],
        'paypal_email' => $request['paypal_email'],
        'password' => Hash::make($request['password']),
    ]);


}    

to this:

public function create(Request $request) {
    $create = User::create([
        'email' => $request['email'],
        'paypal_email' => $request['paypal_email'],
        'password' => Hash::make($request['password']),
    ]);
    if($create){
       return redirect('/home');
    }

    //Return errors here  
}

To learn more about laravel redirects, click here

Related Questions

Sponsored Content

28 Answered Questions

[SOLVED] How can I prevent SQL injection in PHP?

29 Answered Questions

[SOLVED] How do I make a redirect in PHP?

  • 2009-04-20 14:13:22
  • Sam
  • 2771538 View
  • 1223 Score
  • 29 Answer
  • Tags:   php redirect

17 Answered Questions

[SOLVED] How can I sanitize user input with PHP?

3 Answered Questions

laravel 5.8 Auth/RegisterController custom redirect on validation failure

2 Answered Questions

Redirect to login page after a user is registered. laravel 5.5

  • 2019-02-25 14:54:38
  • Samuel Oshio
  • 410 View
  • 0 Score
  • 2 Answer
  • Tags:   php laravel

1 Answered Questions

1 Answered Questions

1 Answered Questions

2 Answered Questions

1 Answered Questions

[SOLVED] Laravel Spark redirect on registration

Sponsored Content