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

LaraBABA's avatar

PHPStorm not helping to find the right namespace

Hello,

I have just purchased PHpStorm based on Jeffrey's feedback but I struggle to get something working .

Lets say you create a function with:

$markers = DB::table('markers')->where....and so on.

PHP Storm will tell you that the class DB is undefined, is there a way for PHPStorm to actually help you in finding it? ie, tell you that:

use Illuminate\Support\Facades\DB;

Is missing at the top of the page

This would be great if it could do that....

Thank you

0 likes
11 replies
ejdelmonico's avatar

@Boubou First you need to set the correct php interpreter and where your composer executable is located. Same for Javascript and node. That should be set in file > defaults. For the project, you just check to make sure they are correct and if using JS then enable node and npm. Also, enable Laravel plugin and check that Blade is enabled. Under Directories, you need to make app a source. After you add app, click the tiny P and type App (capitial A) as an alias. If testing, you add test as Tests and make the capitalized alias for psr-o and psr-4. If you phpstorm to find paths then you also need to add public directory as a resource root. After you are done that you need ide_helper so that phpstorm can discover Laravel classes and subclasses. Just add it a make sure you run the proper commands so that the autoload file is recompiled and a meta file for phpstorm's reference is established.

2 likes
LaraBABA's avatar

@laimist

Thanks I did not know about that I had to install this package, I have just installed it....so if I understand I will need to type this command php artisan ide-helper:meta to get this to work.

@atthew.erskine Thanks, Yes I had already watched this video but it did not give enough help.

@ejdelmonico, thanks for that but I am not sure I followed all the way.... 1)The PHP interpreter has been set 2)Laravel plugin has been installed. 3)Blade is enabled

  1. "Under Directories, you need to make app a source" ->Done, I have also set the alias to "App"

  2. I am not sure I understand this part "If you phpstorm to find paths then you also need to add public directory as a resource root." Shall I right click the "public" folder and click on "resource root"?

Thanks

LaraBABA's avatar

ok I have restarted the IDE and now it seems to be working.

Question for you please, when I typed :

$validator = Validator::make

The IDE proposed "use Dotenv\Validator;" instead of "use Validator;"

Is this ok? I have never seen the "Dotenv" before.

Thank you.

LaraBABA's avatar

Something I have noticed which is strange, no idea if this happens to you but now the IDE detects for example the use Illuminate\Support\Facades\DB; with competing classes and shows the warning:

mutliple definitions exist for class DB.

I clicked "control + B" on the class to see what is going on and found out that this new plugin adds a file called :

_ide_helper.php in the root of the website with:

    class DB {}

So the IDE now see the BD class being called from facade and the helper and shows a warning.

What can I do to solve this please? After that everything else will be absolutely perfect!

Thank you.

ejdelmonico's avatar
Level 53

Yes, suggestions will be a choice you have to make. The IDE picks anything that may apply which makes phpstorm very flexible. The resource root part I mentioned is for when you make relative paths. If you don't tell the IDE where the root is for the public relative path then it will not give you autocompletion and will show a patch error.

1 like
LaraBABA's avatar

Got it, thank you so much Ejdelmonico!

LaraBABA's avatar

I have noticed that if I create a folder in the controllers folder as:

Controllers/Admin Controllers/User

The namespace auto-completion fails again.

Cronix's avatar

It could be bc you have a space in a directory name. I'd change "Admin Controllers" to just "Admin". It's also kind of redundant to say controllers/admin controllers. You can assume that if there is an Admin dir within Controllers, it's Admin Controllers. "Controllers/Admin/User" is pretty self explanatory and logical without being redundant.

1 like
danish981's avatar

Make user vendor directory is there. I was facing the same issue when working with phpstorm2019.1.x and I fixed that issue by indexing the code two or three times, after the indexing, phpstorm started understanding namespaces of all the controllers, models, helpers and facades too (I don't know how but it worked)

For extra autocompletes, you need to install composer package BarryVDH IDE-Helper package or there is a paid plugin called Laravel Idea

Now PhpStorm has evolved too much, it deeply understands your code

1 like

Please or to participate in this conversation.