-1
public function register(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|max:191',
            'name' => 'required|max:191',
            'name' => 'required|max:191',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'validation_errors' => $validator->messages(),
            ]);
        } else {
            $user = User::create([
                'name' => $request->name,
                'email' => $request->email,
                'password' => Hash::make($request->password)
            ]);
            $token = $user->createToken($user->email . '_Token')->plainTextToken;
            return response()->json([
                'status' => 200,
                'username' => $user->name,
                'token' => $token,
                'message' => 'Registered Successfully',
            ]);
        }

Undefined method 'messages'.intelephense(1013),

Undefined type 'App\Http\Controllers\API\User'.intelephense(1009)

(User::create) ($validator->messages()) How to solve these problems? Help please.

  • Did you add `use App\Models\User;` (or `use App\User;`, depending on Namespace) and `use Illuminate\Support\Facades\Validator;` to the top of your code? – Tim Lewis Dec 17 '21 at 15:46
  • use Illuminate\Support\Facades\Validator; I added this. But I didn't add use App\Models\User; – Mehdad Hussain Dec 17 '21 at 15:50
  • Thanks a lot, it is solved. I thought it will auto import. – Mehdad Hussain Dec 17 '21 at 15:52
  • You generally can't call `Model::create` without first importing `Model` via `use App\Models\Model;`, which is why Intellephense is complaining about no type `App\Http\Controllers\API\User`. As for the `messages()` one, I don't think there is a `messages()` method; did you mean `$validator->errors()`? – Tim Lewis Dec 17 '21 at 15:53
  • Yes that one, you saved me.(A lot of time from searching). I am new to laravel. Thanks again. Why you didn't answer it, Instead comment ? – Mehdad Hussain Dec 17 '21 at 15:56
  • Typically I vote to close these kinds of questions as either duplicates or typos/not reproducible. I can convert these comments to an answer though; gimme a sec. – Tim Lewis Dec 17 '21 at 15:57
  • No need you can delete this question or I can . – Mehdad Hussain Dec 17 '21 at 16:00
  • I can't; so you go ahead Cheers for getting this working! – Tim Lewis Dec 17 '21 at 16:01

1 Answers1

0

Firstly Use

use App\User; in your controller and then above that use

use Illuminate\Support\Facades\Validator;

it should fix your problem

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 26 '22 at 07:43