How to use laravel RegisterController without standard auth (php artisan make:Auth) Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to use laravel RegisterController without standard auth (php artisan make:Auth) without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I thought that if($this->validator($request->all())) this would return true or false, but it returns a object every time returns true

use RegistersUsers;

protected $redirectTo = '/home';

    public function __construct()
    {
        $this->middleware('guest');
    }


    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'surname' => 'required|string|max:255',
            'age' => 'required|integer|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ]);
    }


    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'surname' => $data['surname'],
            'age' => $data['age'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

    public function register(Request $request){

        if($this->validator($request->all())){
            //create user
            // function after registration
            var_dump('success');
        }else{
            //redirect to ...
            var_dump('fail');
        }
    }

Answer

You have a couple of options here:

Use validate()

public function register(Request $request)
{
    $this->validator($request->all())->validate();

    dd('success'); //This won't get called if validation doesn't pass
}

Using the above method, Laravel will handle the response for you.


or if you want to have more control over the how you handle the validation then you can use the passes() method instead:

public function register(Request $request)
{
    if ($this->validator($request->all())->passes()) {
        dd('success');
    } else  {
        dd('fail');       
    }
}

Manually creating validators

We are here to answer your question about How to use laravel RegisterController without standard auth (php artisan make:Auth) - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji