Laravel method autocomplete problem

Dear all ,

Why the method autocomplete is not working? 

Please advice,

PHPStorme 2022 latest

11 comments
Comment actions Permalink

Hi there,

Why the method autocomplete is not working? 

Because PhpStorm does not know what the Request is. You need to import the Request class first. You know, have the use Illuminate\Http\Request; kind of line...

1
Comment actions Permalink

Oh Thanks, that's correct,

I thought phpstome would auto-import the class since I checked the Machine Learning-Assisted Completion.

Any idea how "Machine Learning-Assisted Completion" works?

0
Comment actions Permalink

As you can see from your screenshot, the Machine Learning-Assisted Completion is used for sorting the suggestions only. It predicts what elements are likely to be selected in this place (based on your current code and past selections) and offers them higher in the completion popup.

https://www.jetbrains.com/help/phpstorm/auto-completing-code.html#ml_completion

 

Import and Auto-Import is a different thing. The IDE should auto import (add the use line) the class when you select it in the completion popup. At very least it does just that for me.

https://www.jetbrains.com/help/phpstorm/creating-and-optimizing-imports.html

https://www.jetbrains.com/help/phpstorm/creating-and-optimizing-imports.html#completeShortClassName

 

You can also import the class via Quick Fix/Intentions menu (press Alt+Enter (or via light bulb icon) while caret is on the warning/error)

https://www.jetbrains.com/help/phpstorm/creating-and-optimizing-imports.html#import-php-namespace

0
Comment actions Permalink

Please Andriy Bazanov have a look here, I note in many cases PHPstom can not autocomplete. at the same time, the code is working just fine.

e.g , I want to type 

Qr::whereUserId(Auth::id())->paginate(10),

 

When I Type 

Qr::w [ctrl+Space]

I have the following menu but without the whereUserId context

 

Moreover 

if I type 

Qr::whereUserId(Auth::id())-> [ctrl+Space]

Please note that phpstorm reports no suggestions while it has to give me a menu with 

paginate

method included,

0
Comment actions Permalink

UPDATE:

Maybe PHPstorm can not follow the dynamic where classes 

but what about 

Why paginate method is not available?

'qr_list' => Qr::where('user_id', '=', Auth::id())->paginate(10)
0
Comment actions Permalink

@Beshoo

Laravel uses a lot of magic (e.g. calling real methods via static syntax, making up methods from other/partial names etc) or does not specify the concrete return types in their methods (they are dynamic or depend on the parameter value for example). And PhpStorm does not offer any Laravel-specific support (only Blade files). You would need to use a Laravel-specific plugin that knows how Laravel does its magic/works internally and therefore provides such specific completion entries in the right places.

Laravel Idea plugin is very good plugin (supports A LOT of features).. but it's PAID one. I still suggest you install and try it (14 days trial period AFAIK). It well worth the money for Laravel devs.

(P.S. There is also Laravel plugin, but it is no longer in development for quite some time (GitHub repo is in archived/read-only state now) and is incompatible with PhpStorm 2022.2 version)

0
Comment actions Permalink

I already have the Laravel Idea plugin.
But as you can see, auto complete is not working properly

0
Comment actions Permalink

I suggest you check it with Laravel Idea plugin author. The completion for Laravel magic methods (and how that magic gets resolved into the concrete types) is done by that plugin. If plugin reports that some method returns incorrect/no type at all/mixed type in the first place then what PhpStorm can do here?

where() and whereUserId() are not native methods of the Model class. where() is provided by the QueryBuilder, not Model; it gets dynamically resolved/routed by Laravel at runtime only. Such magic resolving is what Laravel specific plugin does (should do).

My suggestion is: make up an example code (so it can be copy-pasted and checked on another machine) and contact the plugin author. If after checking all that he say "all good on my end, it's PhpStorm that messing up/gets confused later" then it will need to be reported to PhpStorm Issue Tracker. Or he may tell you what else you need to do/add in your class to have such methods get recognized.

https://github.com/laravel-idea/plugin/issues

But before that, if you have not done it yet, please check the plugin docs -- you may have missed an extra step to have such code recognized. https://laravel-idea.com/docs/eloquent

0
Comment actions Permalink

Andriy Bazanov

Thank you so much for the information.

Well, I tested the "extra step" and it works great.

I wonder why PHPstrom does not adopt this add-on as a part of the program, not as a paid plugin. since Laravel redefines PHP programing path.

I hope that PHPstrom conceded this Laravel Idea plugin as a part of the IDE.

0
Comment actions Permalink

1. Because it's a PAID plugin made by 3rd party and it exists already.

2. PhpStorm does not offer anything special for Symfony, Yii, Phalcon, Zend and other PHP frameworks. Why it should provide such special support for Laravel?

3. But perhaps if creators/owners of Laravel framework contact JetBrains and come to some mutual agreement/deal then we may see it one day, why not.

1
Comment actions Permalink

You made a good point.

Thanks so much for your time and efforts in describing the situation.

With Love.

0

Please sign in to leave a comment.