最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Undefined method 'usersCoolPosts', but both the project and method is workingrunning fine - Stack Overflow

programmeradmin5浏览0评论

The intelephense tells me that I have an undefined method which is 'userCoolPosts' from routes/web.php, but as you can see below, the method exists in the User.php from app/Models folder. Upon testing my project, the method is working fine but the error in the console persists.

web.php

Route::get('/', function () {
    $posts = [];
    if (Auth::check()) {
        $posts = Auth::user()->usersCoolPosts()->latest()->get();
    }
    return view('home', ['posts' => $posts]);
});

User.php from app/Models folder

    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
        ];
    }

    public function usersCoolPosts()
    {
        return $this->hasMany(Post::class, 'user_id');
    }
}

I did reindex Intelephense (index workspace), and still the error persists.

The intelephense tells me that I have an undefined method which is 'userCoolPosts' from routes/web.php, but as you can see below, the method exists in the User.php from app/Models folder. Upon testing my project, the method is working fine but the error in the console persists.

web.php

Route::get('/', function () {
    $posts = [];
    if (Auth::check()) {
        $posts = Auth::user()->usersCoolPosts()->latest()->get();
    }
    return view('home', ['posts' => $posts]);
});

User.php from app/Models folder

    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
        ];
    }

    public function usersCoolPosts()
    {
        return $this->hasMany(Post::class, 'user_id');
    }
}

I did reindex Intelephense (index workspace), and still the error persists.

Share Improve this question edited 10 hours ago hakre 198k55 gold badges446 silver badges854 bronze badges Recognized by PHP Collective asked 10 hours ago JukKie-aiJukKie-ai 132 bronze badges New contributor JukKie-ai is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 4
  • I'll just leave this alone, then? Will there be a time that this error will 'magically' disappear? – JukKie-ai Commented 10 hours ago
  • 1 Have you tried adding an @var docblock for Auth::user() to type-hint it as a User model? – Matthew Bradley Commented 10 hours ago
  • @JukKie-ai: Please check stackoverflow/q/78716482/367456 on how you can extend intelephense for laravel, access the log of intelephense, and if it does not yet resolve the issue for you, edit the question here and add information from the log (please as text, not as image, that is a bad example in the answer over there). – hakre Commented 10 hours ago
  • @hakre, the answer that you provided below fixed my issue! thanks a lot, cheers! – JukKie-ai Commented 9 hours ago
Add a comment  | 

1 Answer 1

Reset to default 1

Auth::user() returns \Illuminate\Contracts\Auth\Authenticatable|null. Which does not have your method.

You can safely ignore this message. Personally I use https://github/barryvdh/laravel-ide-helper Which resolves such issues by rewriting the expected output to your User model.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论