Matthias
Matthias

Reputation: 33

Laravel: auth()->attempt undefined method

Everything seems to work, but VSCODE keeps reporting an error on attempt. Saying that the method has not been defined.

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class RegisterController extends Controller
{
    public function index()
    {
        return view('auth.register');
    }

    public function store(Request $request)
    {
        //validation
        $this->validate($request, [
            'name' => 'required|max:50',
            'username' => 'required|max:50',
            'email' => 'required|email|max:250',
            'password' => 'required|confirmed',
        ]);
        //create new user
        User::create([
            'name' => $request->name,
            'username' => $request->username,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);

        auth()->attempt($request->only('email','password'));

        return redirect()->route('dashboard');

        //sign the user in
        //redirect
    }
}

is it a vscode problem or am I missing something?

Upvotes: 3

Views: 8970

Answers (2)

s3c
s3c

Reputation: 1851

So I think I followed the same tutorial of TraversyMedia's guest Alex, and stumbled upon the very same problem.

While suppressing diagnostics obviously work, it's not quite what I want (would not accept).

In this case the method attempt exists, so I just want the awesome Intelephense extension to recognize it.

I'll try to explain so it's applicable to more than just this case.

Solution

Ctrl + click on auth() which is supposed to have the method brings us to helpers.php file where the function is defined.

There we see a small bit of code with an if statement, but either way it returns the same class, namely AuthFactory.

So again we Ctrl + click on AuthFactory and we find ourselves staring at Factory interface namespaced Illuminate\Contracts\Auth;

Now we can use PHPDocs to tell Intelephense that a method named attempt can be used on an instance of this interface. Note the "comment" (PHPDocs) just above interface Factory declaration in the full code below.

<?php

namespace Illuminate\Contracts\Auth;

/**
 * @method attempt
 */
interface Factory
{
    /**
     * Get a guard instance by name.
     *
     * @param  string|null  $name
     * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
     */
    public function guard($name = null);

    /**
     * Set the default guard the factory should serve.
     *
     * @param  string  $name
     * @return void
     */
    public function shouldUse($name);
}

Voilà !

Note

You should only do this if the method really does exist, otherwise you're nailing your own coffin.

Upvotes: 9

Kamlesh Paul
Kamlesh Paul

Reputation: 12391

Here is I solved:

Open the extension settings:

enter image description here

And open json of setting

enter image description here

The variables you should consider are:

"intelephense.diagnostics.undefinedTypes": false,
"intelephense.diagnostics.undefinedFunctions": false,
"intelephense.diagnostics.undefinedConstants": false,
"intelephense.diagnostics.undefinedClassConstants": false,
"intelephense.diagnostics.undefinedMethods": false,
"intelephense.diagnostics.undefinedProperties": false,
"intelephense.diagnostics.undefinedVariables": false,  

then press control + shift+p and search for intelephense then select index worksapce

Upvotes: 4

Related Questions