Search code examples
phplaravellaravel-5idephpstorm

scopeMethods not found in controller PhpStorm Laravel 5


I'm new to PHP and Laravel so perhaps I'm doing something wrong. I'm following along to Jeffrey Way's Laracasts tutorials and he creates a "query scope" method.

When I then use that in my controller, it says it cant find it.

In my Task model I have:

public static function scopeCompleted( Builder $query )
{
    return $query->where( 'completed', 1 );
}

In my controller I have:

$completed_tasks = Task::completed()->get();

but completed is highlighted by PhpStorm with the error:

"Method 'completed' not found in App\Task"

I know it's looking for a method called scopeCompleted, but this is not how you call a query scope method in the controller.

Am I doing something wrong or is this just a flaw in PhpStorm?

FYI: the code functions perfectly well.


Solution

  • This is because behind the scenes, Laravel uses magic methods to resolve your scope.

    The completed method doesn't actually exist in your model, so technically, PHPStorm is correct.

    If you want PHPstorm to be able to detect this scope, then install https://github.com/barryvdh/laravel-ide-helper into your project.