10

I'm working on a CMS and I have a little problem with my migrations. I added a new migration file and I wanted to add that one. That didn't work so I ran this bit:

php artisan migrate:reset

After that I ran this bit:

php artisan migrate:install
php artisan migrate

And now I get this error:

{"error":{"type":"Illuminate\\Database\\QueryException","message":"SQLSTATE[42S02]: Base table or 
view not found:1146 Table 'cms.pages' doesn't exist (SQL: select * from `pages`)"

The error kinda tells me that it can't find the database, because that's true.

I also have a command that runs the migrate and I run that one like this:

php artisan app:install

But that shows the same error...

bobbybouwmann
  • 983
  • 3
  • 12
  • 24
  • 2
    do you have any custom service providers added to `app/config/app.php`? They still run during artisan commands, and if one of them depends on the pages table existing you will run into this situation. – Jeff Lambert Aug 14 '14 at 18:40
  • Nop, I don't... I did made my own SiteServiceProvider: package('app/site', 'site', public_path() . '/site'); } public function register() { require public_path() . '/site/routes.php'; } } – bobbybouwmann Aug 14 '14 at 18:45
  • unless you explicitly added it to the app config, it won't be included. another possibility would be something added to `bootstrap/start.php`, `app/start/global.php`, `app/start/local.php` or `app/routes.php` that is looking for that table – Jeff Lambert Aug 14 '14 at 18:50
  • I do use the laravel-menu provider and I generate a menu out of pages in my app/routes.php, can that be it? – bobbybouwmann Aug 14 '14 at 18:50
  • yes, the routes still get processed even during artisan commands. thats how `$ php artisan routes` knows the routes :) – Jeff Lambert Aug 14 '14 at 18:52

2 Answers2

13

Remove any lines requesting data from your model from these files to be sure artisan is not trying to load data from your non-existent table:

  • bootstrap/start.php
  • app/start/global.php
  • app/start/local.php
  • app/routes.php

Also be sure to un-register any service providers that utilize data from that table in their register or boot methods inside of app/config/app.php.


The issue is that these files not only get executed for browser (web) requests, but for all requests, including command-line artisan invocations (e.g. php artisan migrate). So if you try to use something before it is available in any of these files, you are going to have a Bad Time.

Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
  • Let me try this out :D – bobbybouwmann Aug 14 '14 at 18:55
  • Well that's the answer :D Thanks man! Do you have any idea how I can prevent this in the future with my laravel/menu (this is the repository: https://github.com/lavary/laravel-menu) – bobbybouwmann Aug 14 '14 at 18:58
  • Perhaps, I haven't looked too deeply at that, but I would define a generic 'Page' route only, with parameters to the route and let the controller that route points to fetch data from the model. I would register the menus in a service provider. you would still have to unregister that service provider when you need to migrate from scratch again, but its a lot less destructive than clearing out your routes, imho. – Jeff Lambert Aug 14 '14 at 19:02
  • Your suggestion pointed me in the right direction. My issue was I was referencing a Model in the AuthServiceProvider before the table existed. – MMMTroy Nov 27 '15 at 18:36
  • thanks man it works, in my case (L5) i requesting data from `app/Providers/AppServiceProvider.php` – Firman Hidayat Sep 26 '16 at 04:59
  • I am having a bad time with this – Pathros Jul 08 '17 at 18:52
2

You can use this to dictate when your app is running from the console. I believe this issue only occurs when you run a command

if( !App::runningInConsole() ){
  //allow laravel-menu to run
}

This way you will prevent data load from your non-existent table

Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96