0

I'm getting a weird error trying to use PHP Artisan. I don't know the cause, or even which file is causing it.

Just a few moments ago I ran php artisan migrate:reset to wipeout all the tables in my db. Worked fine.

Then I made a change to a file completely unrelated to the "stages" table, and when I try to run php artisan migrate it throws an error saying:

SQLSTATE[42S02]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid object name 'stages'. (SQL: select * from [stages] where [stages].[deleted_at] is null order by [opord] asc)

It throws that error no matter what I try to do, even just typing php artisan causes that fatal error.

Also, if I manually add a table called "stages" to the DB, artisan doesn't fail immediately, it tries to migrate the tables, then fails when it gets to the stages table, and it says it failed because "stages" already exists.

I don't even understand why artisan is trying to perform a select statement when I'm just trying to migrate my tables; and I especially don't understand why it's happening when running the php artisan command alone.

EDIT:

I traced the problem to some code that I had added to app\Providers\AppServiceProvider

public function boot(){
    View::share('nav_stages', Stage::opord()->get());
}

I found a SO question about how to check if a table exists and I'm currently trying to make this work:

public function boot(){
    if (Schema::hasTable('stages'))
    {
        View::share('nav_stages', Stage::opord()->get());
    }else{
        View::share('nav_stages', null);
    }
}
Community
  • 1
  • 1
Brent Connor
  • 628
  • 2
  • 8
  • 23

1 Answers1

0

The problem was caused by a bit of code I had in AppServiceProvider.php In the code I was trying to query the DB for data required by the navigation menu used on the entire site (a master page, layout, partial view, etc).

I replaced

public function boot(){
    View::share('nav_stages', Stage::opord()->get());
}

With

public function boot(){
    if (Schema::hasTable('stages'))
    {
        View::share('nav_stages', Stage::opord()->get());
    }else{
        View::share('nav_stages', null);
    }
}

And added

use Illuminate\Support\Facades\Schema;

at the top of the file

Brent Connor
  • 628
  • 2
  • 8
  • 23