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

gnack's avatar

php artisan - unknown table error on any command

For some reason whenever I run any php artisan command I get an error saying one of my tables doesn't exist. This is true, I'm starting on a new database. What I can't figure out is why on earth artisan needs to use this table? The error that shows up is:

[Illuminate\Database\QueryException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table
    'happylister.categories' doesn't exist (SQL: select *
    from `categories` order by `name` asc)

[PDOException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table
    'happylister.categories' doesn't exist

I just can't figure out for the life of me where that query is coming from - even just using php artisan generates this error. I first noticed this when I tried to run php artisan migrate to set up the tables, which of course failed. Then I noticed ALL php artisan commands fail.

So my question is: why would php artisan need a table specific to my app, given that normally you should be able to use php artisan on a clean database to set it up?

0 likes
6 replies
fetch404's avatar
Level 7

If you're referencing a model from a service provider and the migrations have not been run, then you'll get that error. For some reason Artisan commands like to load all the providers, even ones it doesn't need.

7 likes
gnack's avatar

@FetchTheDev you're a genius! Thank you, thank you, thank you!!

So if that's the case, how should I make service providers in such a way that a fresh install of my app with an empty database won't create artisan errors?

fetch404's avatar

This may not be the best way, but I do it: Use "Schema::hasTable('my_table')" with an if check and do your model stuff inside of that. Foolproof way to get no errors (unless your database details are invalid!)

Example:

if (Schema::hasTable('categories'))
{
    // Do your model stuff
}

Also, please mark my original post as the correct answer if I helped :)

1 like
gnack's avatar

My apologies - not familiar with this forum system. :)

Thanks for the suggestion, I can give that a go.

foerno's avatar

in my case it was a console command in app/Console/Commands that was referencing an inexistent table. i fixed the issue by commenting the command in my app/Console/Kernel.php file, then running migrations, then uncommenting the line again.

1 like
AKOPBr's avatar

@fetch404 I'm just posting because your answer helped me a lot =)

if (Schema::hasTable('categories')) { // Do your model stuff }

Please or to participate in this conversation.