Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mylo_92's avatar

Separating Request Data

Good Day,

In the built-in Laravel Authentication/Registration function, the postRegister() method accepts a request with inputs from the form in register.blade.php. These inputs being:

'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',

I am able to register a user using the this function, however, I expanded and have other fields present e.g employed, qualification level for a profile, in my form that also require validation. The postRegister() method, further includes the line: Auth::login($this->create($request->all())); which logs a user in after successful registration. How do I exclude inputs from my request other than the ones stated above for this to be successful? Every time I try and do so, the other input fields present in my form can't seem to be found. Moreover, if i leave my request as is, I get the following error:

ErrorException in Guard.php line 430:
Argument 1 passed to Illuminate\Auth\Guard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, instance of Illuminate\Http\RedirectResponse given

, however, my user is created along with their profile. On the other hand is there any way to override, remove or simply make the: Auth::login($this->create($request->all())); line not execute?

Regards, Mylo

0 likes
8 replies
starbolt's avatar

You can use

$this->request->only('key1', 'key2')

So you will only return the keys defined.

Mylo_92's avatar

@starbolt Let's say I want to send a verification email upon successful registration and thus do not want the user to log in immediately. Rather, after clicking on an activation link. How would one override that line or rather, like I said, make it not execute?

starbolt's avatar

Can you post the entire code here? I only see the validator rules.

Mylo_92's avatar

The validator:

protected function validator(array $data)
    {
        return Validator::make($data, [
            'first_name'                 => 'required|min:5|max:20',
            'last_name'                  => 'required|min:5|max:25',
            'email'                      => 'required|email|max:255|unique:users',
            'title'                      => 'required|in:Mr.,Ms.,Mrs.,Dr.',
            'gender'                     => 'required|in:Female,Male',
            'ethnicity'                  => 'required|min:5',
            'dob'                        => 'required|date',
            'contact_no'                 => 'required|numeric', 
            'degree_discipline'           => 'required|not_in:0',
            'level'                       => 'required|not_in:0',
            'articles'                    => 'required|in:Yes,No',
            'articles_discipline'         => 'required_if:articles,==,1|not_in:0',
        'resume'                    => 'required|mimes:pdf|max:2048',
            'pro_qual'                        => 'required|in:Yes,No',
            'professional_qualification_discipline' => 'required_if:pro_qual,==,1|not_in:0',
        //etc
        ]);
    }

This is my create method:

protected function create(array $data)
    {
        $user = new User(array_only($data,['first_name','last_name','email']));
        $p = str_random(10);
        $user->password = bcrypt($p);
        $code = str_random(60);
        $user->activation_code = $code;
        $user->active = false;
        $user->first_login = true;
        $profile= new Applicant(array_except($data,['first_name','last_name','email']));
     
        $resume = $data['resume'];
        $destinationPath = app_path() . '/storage/resumes/'; // upload path
        $extension = $resume->getClientOriginalExtension(); // getting image extension
        $fileName = $user->email.'.'.$extension; // renameing image
        $resume->move($destinationPath, $fileName); // uploading file to given path
        $profile->resume = $destinationPath .'/'. $fileName;
        $user->save();
        $user->attachRole(3);
        $user->profile()->save($profile);
        
        if($user){
            if($profile){
                 
                Mail::send('auth.emails.activate', ['link' => route('activate', $code),'first_name' => $user->first_name,'last_name' => $user->last_name, 'password' => $p], function($message) use($user) {
                    $message->to($user->email, $user->first_name, $user->last_name)->setSubject('Activate your account');
                });

                return redirect('auth/login');
                   Flash::overlay('Registered', 'Your profile has been registered please check your email for log in details'); 
            }
         return redirect('auth/register');
         Flash::overlay('Error', 'Your account could not be created! Please try again');
        }
        return redirect('auth/register');
         Flash::overlay('Error', 'Your account could not be created! Please try again');
    }

The default postRegister() method:

public function postRegister(Request $request)
    {
        $validator = $this->validator($request->all());

        if ($validator->fails()) {
            $this->throwValidationException(
                $request, $validator
            );
        }

        Auth::login($this->create($request->all()));

        return redirect($this->redirectPath());
    }
starbolt's avatar
Level 6

You can override the postRegister() method on your Auth Controller.

There you can use your own logic.

//AuthController.php
public function postRegister()
{
    //
}
Mylo_92's avatar

@starbolt I understand that part but what if I only need the Auth::login($this->create($request->all())) method not to fire? I have tried simply pasting the postRegister() method from RegistersUsers.php and removing that specific line, however now I keep getting redirected to the same page: auth/register. In essence, I want the that line removed from the logic totally or rather to be able to pass the user variable from create which already includes the necessary fields to log in. What is the best way to achieve this? My preferred result would be to remove the Auth::login() method totally from the logic.

Francismori7's avatar

Simply overwrite the method in AuthController, copy and paste the original and make your own modifications as you wish.

1 like
starbolt's avatar

@Mylo_92 have you tried overwritting the method? You can simply paste this logic and remove this line. Tell me if it works.

Please or to participate in this conversation.