Tom Morison Tom Morison - 7 months ago 41
PHP Question

laravel - php artisan migrate fails

I have accidently deleted a database migration table and I have to rollback my changes. This ends up failing. When I load the items view I get the following error.


QueryException in Connection.php line 669: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ushop.items' doesn't exist (SQL: select * from
items
)


in Connection.php line 669
at Connection->runQueryCallback('select * from `items`', array(), object(Closure)) in Connection.php line 629
at Connection->run('select * from `items`', array(), object(Closure)) in Connection.php line 342
at Connection->select('select * from `items`', array(), true) in Builder.php line 1461
at Builder->runSelect() in Builder.php line 1447
at Builder->get(array('*')) in Builder.php line 569
at Builder->getModels(array('*')) in Builder.php line 303
at Builder->get(array('*')) in Model.php line 646
at Model::all() in ItemController.php line 15
at ItemController->index()
at call_user_func_array(array(object(ItemController), 'index'), array()) in Controller.php line 80
at Controller->callAction('index', array()) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(ItemController), object(Route), 'index') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(ItemController), object(Route), object(Request), 'index') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\ItemController', 'index') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 724
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54
at require_once('/home/thomas/ushop/public/index.php') in server.php line 21


r
I have done some research and I have found this and I get the following upon running composer dump-autoload


PHP Fatal error: Class 'Item' not found in /home/thomas/ushop/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php on line 336


[Symfony\Component\Debug\Exception\FatalErrorException]

Class 'Item' not found

I hope this question hasn't been answered before, but I'm not having much luck so far.

Answer

It's because you've manually deleted a table in the database so right now the table is not available there but when you try to rolling back the framework trys to undo the migration and hence it looks for the table in the database you've deleted manually so the error occurs. Why it looks for the table because in the down method of your migration file you've something like this:

Schema::drop('table_name');

So, it trys to delete the already deleted table from the database but it couldn't find and in your migrations table there is a record for every table you've migrated so you can either manually delete all tables and migrate from the beginning or disable the down (By commenting the drop statement) method temporarily so down method will do nothing in that migration file when you roll back. If you disable the down method temporaarily re-enable the down method again. It's a hack-ish way to fix the problem tho.

Alternatively, you can add a check in your down method in every migration file or just the deleted table's migration file by checking if the table exists then drop it, for example:

public function down()
{
    if (Schema::hasTable('table_name')) {
        Schema::drop('table_name');
    }
}

Hope you've got the idea.