Laravel User Class was not found

I ran into a Laravel problem trying to access the build-in class on Laravel named ‘User’, basically I want to create new users. But I get the error saying this: ‘Undefined type ‘AppHttpControllersUser’.intelephense(1009)`. What is the fix?

<?php

namespace AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesValidator;

class AuthController extends Controller
{
    public function register(Request $request){
        $validator = Validator::make($request->all(),[
            'name' => 'required',
            'email' => 'required|email',
            'password' => 'required'
        ]);

        if($validator->fails()){
            return response()->json(['status_code']);
        };

        $user = new User();
        $user->name = $request->name;
        $user->email = $request->email;
        $user->password = $request->password;

        return response()->json([
            'status_code' => 200,
            'message' => 'User was saved!'
        ]);
    }
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You forgot to import the User class:

<?php

namespace AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesValidator;

// this is the default for Laravel 8+, before it was AppUser
use AppModelsUser; 

class AuthController extends Controller
{
    public function register(Request $request){
        $validator = Validator::make($request->all(),[
            'name' => 'required',
            'email' => 'required|email',
            'password' => 'required'
        ]);

        if($validator->fails()){
            return response()->json(['status_code']);
        };

        $user = new User();
        $user->name = $request->name;
        $user->email = $request->email;
        $user->password = $request->password;

        return response()->json([
            'status_code' => 200,
            'message' => 'User was saved!'
        ]);
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x