Search code examples
phplaravellaravel-5.4email-verification

laravel 5.4: Undefined property: Illuminate\Validation\Validator::$errors


I have here a code in email validation wherein users register and upon registering he would accept a registration confirmation email. I'm using laravel make auth with modification to include sending tokens to valid email. Upon successful registering a confirmation message should appear but it wouldn't.

here's the code:

RegisterController

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Mail;
use Illuminate\HTTP\Request;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
    protected function register(Request $request){
        $input = $request->all();
        $validator = $this->validator($input);

        if ($validator->passes()){
            $data = $this->create($input)->toArray();
            $data['token'] = str_random(25);

            $user= User::find($data['id']);
            $user->token=$data['token'];
            $user->save();

            Mail::send('mail.confirmation', $data, function($message) use($data){
                $message->to($data['email']);
                $message->subject('Registration Confirmation');

            });
            return redirect(route('login'))->with('status','confirmation email has been sent');
        }
        return redirect(route('login'))->with('status', $validator->errors);
    }
    public function confirmation($token)
    {
        $user = User::where('token', $token)->first();
        if(!is_null($user)){
            $user->confirmed=1;
            $user->token='';
            $user->save();
            return redirect(route('login'))->with('status','complete actibeyt');
        }
        return redirect(route('login'))->with('status','something is wrong');
    }
}

Solution

  • There isn't a property on the Validator called errors, however, there is a method.

    Simple change:

    return redirect(route('login'))->with('status', $validator->errors());