Base table or view not found in Laravel 5.3

Posted on

This is an article which is focusing on an error generated as “Base table or view not found” as soon as the development of web application using Laravel 5.3 as the main framework is being executed. The error is found when it is executing the web application and the image showing the error can be shown below :

Base table or view not found in Laravel 5.3
Base table or view not found in Laravel 5.3

It is also shown in the laravel.log file located in storage/logs below :

[2017-02-12 16:38:59] local.ERROR: PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'xxxxx.servers' doesn't exist in /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Database/Connection.php:333
Stack trace:
#0 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Database/Connection.php(333): PDO->prepare('select count(*)...')
#1 /var/www/html/infrastructure/vendor/laravel/framework/src/Illuminate/Database/Connection.php(754): Illuminate\Database\Connection->Illuminate\Database\{closure}(Object(Illuminate\Database\MySqlConnection), 'select count(*)...', Array)
#2 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Database/Connection.php(717): Illuminate\Database\Connection->runQueryCallback('select count(*)...', Array, Object(Closure))
#3 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Database/Connection.php(351): Illuminate\Database\Connection->run('select count(*)...', Array, Object(Closure))
#4 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1648): Illuminate\Database\Connection->select('select count(*)...', Array, true)
#5 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1634): Illuminate\Database\Query\Builder->runSelect()
#6 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1709): Illuminate\Database\Query\Builder->get()
#7 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(479): Illuminate\Database\Query\Builder->getCountForPagination()
#8 /var/www/html/laravel-project/app/Http/Controllers/ServerController.php(24): Illuminate\Database\Eloquent\Builder->paginate(5)
#9 [internal function]: App\Http\Controllers\ServerController->index()
#10 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(55): call_user_func_array(Array, Array)
#11 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(44): Illuminate\Routing\Controller->callAction('index', Array)
#12 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Route.php(189): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\ServerController), 'index')
#13 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Route.php(144): Illuminate\Routing\Route->runController()
#14 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php(642): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#15 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#16 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#17 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(137): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#18 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#19 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(64): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#20 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(137): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#21 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#22 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#23 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(137): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#24 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#25 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(64): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#26 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(137): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#27 /var/www/html/infrastructure/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#28 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#29 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(137): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#30 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#31 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#32 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(137): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#33 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#34 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#35 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php(644): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#36 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php(618): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#37 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php(596): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#38 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(267): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#39 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#40 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#41 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(137): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#42 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#43 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#44 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(149): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#45 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#46 /var/www/html/laravel-project/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#47 {main}

The error generated specifically in the above log is shown below :

Base table or view not found: 1146 Table 'xxxxx.servers' doesn't exist in /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Database/Connection.php:333

The error stated that the table or view cannot be found or it is not exist based on the error shown above. Precisely, in this context, after reading  through the error,  there are further information regarding the cause of the error which is shown below :

Table 'xxxxx.servers' doesn't exist in /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Database/Connection.php:333

It means, in the database named ‘xxxxx’, there is no table named ‘servers’. Further action is to find out where is the place or location which is actually file executed so that it is triggering a query to the table named ‘servers’ in the database ‘xxxxx’.

After searching throughly, the script triggering a specific query on the table named ‘servers’ ‘in the database named ‘xxxxx’ is actually located in the model file named ‘Server’ located in ‘App’ as default. It is shown below :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Server extends Model
{
//
protected $table = 'servers';
}

Indeed, the error is triggered since the table name associated with the model named ‘Server’ and it is used in the controller named ‘ServerController.php’ is not exist. Try to execute the query in MySQL command console to search the existence of the table :

user@hostname:/var/www/html/laravel-project/app# mysql -uroot -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 531
Server version: x.x.xx-x-xxxxx1.1 (xxxxx)

Copyright (c) xxxx, xxxx, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use xxxxx;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-----------------+
| Tables_in_xxxxx |
+-----------------+
| xxx             |
| xx              |
| xxxxxxxxxx      |
| xxxx            |
| xxxxxxxxx       |
| xxxxxxx         |
| server          |
| xxxx            |
+-----------------+
8 rows in set (0,00 sec)

mysql> 

The above output clearly display that the name of the table is not ‘servers’ but it is actually has the name of ‘server’. By editing the file represent the model named ‘Server.php’ as follows :

protected $table = 'servers';

and change it into the following line :

protected $table = 'server';

Since the correct table named as ‘server’, if there aren’t any available errors triggered, the problem will be solved.

One thought on “Base table or view not found in Laravel 5.3

Leave a Reply