1

A few days ago I started learning laravel 7. I bought a course on udemy. I got to the part where the real registry system went and started to rewrite the code like in the video, but when I do it I get an error!

Error Message: "Class 'App\Http\Controllers\Validator' not found"

I've been trying to fix this for hours, and I'm not doing well

AccountController.php

<?php

namespace App\Http\Controllers;

class AccountController extends Controller
{
    public function getcreate(){
        return view('account.create');
    }

    public function postcreate(){
        $validator = Validator::make(Input::all(), 
        array(
            'email'  =>      'required|max:50|email|unique:users',
            'username' =>    'required|max:20|min:3|unique:users',
            'password' =>    'required|min:6',
            'repeat_pass' => 'required|same:password'   
        ));

        if($validator->fails()){
            die('ERROR');
        }
        else{
            die('Cool');
        }
    }
}
1
  • Seems you are using very old tutorial (if you installed Laravel 7 and that you showed is not some inherited code base). Commented Mar 17, 2020 at 23:49

3 Answers 3

4

you need to import validator namespace

use Illuminate\Support\Facades\Validator;

then instead of Input you could use request()->all() helper function

so it will be like this

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Validator;

class AccountController extends Controller
{
    public function getcreate(){
        return view('account.create');
    }

    public function postcreate(){
        $validator = Validator::make(request()->all(), 
        array(
            'email'  =>      'required|max:50|email|unique:users',
            'username' =>    'required|max:20|min:3|unique:users',
            'password' =>    'required|min:6',
            'repeat_pass' => 'required|same:password'   
        ));

        if($validator->fails()){
            die('ERROR');
        }
        else{
            die('Cool');
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

is there any example of writing request-> all () I think I'll do it
Thank you so much I was able to fix this!
@Nik33__ you are welcome :), if it helps accept the answer
0

You will need to import the Validator class from it's correct namespace which is Illuminate\Support\Facades. So goes with Input class. Best way I can suggest is to add these in aliases section in config\app.php like below:

 'aliases' => [
     // other imports
     'Validator' => Illuminate\Support\Facades\Validator::class,
     'Input' => Illuminate\Support\Facades\Input::class,
 ]

Now, you can simply use them in your controller like below:

<?php

namespace App\Http\Controllers;
use Validator;
use Input;


class AccountController extends Controller
{
 // rest of your code
}

Comments

0

You can use Validator namespace in your controller at top like:

use Validator;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.