Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ngocphamm's avatar

Laravel and PHPStorm

I know this has been mentioned a lot, and I've read most of them. However, today, my attempt to return to PHPStorm (when it's version 9 is still hot served) seems to be a failure.

The tutorial from JetBrains looks good, but it didn't help me out. https://confluence.jetbrains.com/display/PhpStorm/Laravel+Development+using+PhpStorm

This is what bugs me with the IDE, in one screenshot. It cannot know what's where and Illuminate\Database\Eloquent\Relations\BelongsTo come from. The silly thing here is I'm able to Cmd + Click the belongsTo in the code return $this->belongsTo('App\User') inside the author() method (not shown in the screenshot) to navigate to the right code/class.

bs

If anyone has been successfully setup PHPStorm to work nicely with Laravel without yelling a lot, please help.

Thank you guys!

P.S: I'm following this series, and on the video, Jeffrey didn't seem to have the problem. Idk if either he turned off PHP inspection or he had his setup done right.

0 likes
12 replies
ngocphamm's avatar

Hi @jimmck. Thanks for the answer! It didn't help, though :(

  1. I installed the IDE helper, ran the generate command(s) several times (and I can se Route facade being recognized correctly by the IDE as the result).

  2. I tried to set the directory similar to the post, but no luck (yet).

  3. Installed Laravel Plugin before. Just got the Symphony 2 Plugin, enabled. Didn't help either. Is there any special configuration I need to take care of for the Symphony plugin?

jimmck's avatar

Well when look at the second post you will see where the direrctories in a project are included and excluded.

What is your setup, Windows, MAC,or Linux. What version of Laravel. You do have composer installed and working.

What your issues are is not clear.

ngocphamm's avatar

I was just not being explicit about the setup. It's Mac, PHPStorm 9.0, and Laravel 5.0. And yes, I do have composer as I used it to install Laravel.

The include and exclude thing under PHPStorm I do understand, and by default, PHPStorm will try to somehow "exclude" the vendor packages, but you still be able to have code completion for that. It's why I still be able to Cmd + Click the belongsTo method to go to the corresponding Illuminate class. It's just 2 things that annoy me, as can be seen from the screenshot.

  1. The where method is marked as not found. It doesn't seem to know the $query is an Illuminate\Database\Query\Builder object. (is it?, I'm new to Laravel anyway).
  2. The docblock yells at me with the red notation that it doesn't know where the Relations part comes from. Apparently, it's not quite true, when, as I stated, I can Cmd + Click to go to that class.
ngocphamm's avatar

Bump! So someone who missed it last night can help me out :P Thanks!

jaewun's avatar

PHPStorm can not know what type of object a variable refers to if it is ambiguous. Your screenshot has expired so I'm unable to see exactly what you're talking about.

PHPStorm isn't always perfect, but it does its best and does much better than the other options I've tried out there.

ian_h's avatar
/** @var \Illuminate\Database\Query\Builder $dbObj */
$dbObj = Foo::findOrFail(1);
$dbObj->increment('counter')
    ->where('foo', '=', 'bar');

Now it knows that $dbObj is an instance of Illuminate\Database\Query\Builder and will correctly handle where, etc etc.

Cheers..

Ian

1 like
ngocphamm's avatar

This does fix the problem, Ian, but it still just a kinda "workaround" and I still don't understand why PHPStorm doesn't take it when I do type hinting as below. It doesn't recognize the Query part of the full namespace, just like what happens with @return Illuminate\Database\Eloquent\Relations\BelongsTo in the docblock.

public function scopePublished(Illuminate\Database\Query\Builder $query)
{
    $query->where('published_at', '<=',  Carbon::now());
}
ngocphamm's avatar

@jaewun I've updated the screenshot. Dropbox seems to auto expire it. Hopefully this will last.

ian_h's avatar

@ngocphamm I don't know why it works (or doesn't, as the case may be).. I've just got into the habit of using the /** @var ... */ notations.. might not be right, as such, but the end result is worth it I think for ease of use.

Cheers..

Ian

ngocphamm's avatar

Yep. Thanks for sharing a workable method, Ian.

I'm just curious of what others' setup so that they can use PHPStorm to comfortably enjoy Laravel. If that contains disabling some sort of PHP inspection, then I will understand and leave it that way.

ian_h's avatar

@ngocphamm FWIW, it's not just Laravel. I've only been using Laravel for a couple of weeks, previously mainly Zend Framework 1 / 2, and it was the exact same for the Zend Frameworks too.. my ZF code's riddled with things like:

/** @var \Some\Namespaced\Class $foo */
$foo = $this->getServiceLocator()->get('Some\Namespaced\Class');

just to make it easier not to have to remember every method of every class =)

Cheers..

Ian

Please or to participate in this conversation.