Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel error: SQLSTATE[42S02]: Base table or view not found

Tags:

php

laravel

The full error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'quotesapp.admin' doesn't exist (SQL: select count(*) as aggregate from `admin` where `username` = Admin)

I know the error is the mismatch between the name as it appears in the error log and how it's defined everywhere else (in the database folder, but I am unable to solve the problem. I searched around and found this post, but even after I implemented the solution(shown below), I keep getting the same error.

I am using Laravel 5.2. I have an admins table in my database directory which looks like this:

class CreateAdminsTable extends Migration
{

    public function up()
    {
        Schema::create('admins', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->string('username')->unique();
            $table->string('password');
            $table->rememberToken();
        });
    }

    public function down()
    {
        Schema::drop('admins');
    }
}

The Admin model looks like this :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Authenticatable; 

class Admin extends Model implements \Illuminate\Contracts\Auth\Authenticatable
{
    protected $table = 'admins';
    use Authenticatable;
}

?>

Here is the relevant route that is causing the error to be thrown:

Route::post('/admin/register', [
    'uses' => 'AdminController@postRegister',
    'as' => 'register'
]);

And here is the postRegister method

 public function postRegister(Request $request) {

    $this->validate($request, [
        'username' => 'required|unique:admin|max:30|min:3',
        'password' => 'required|min:5',
        'password_confirm' => 'required'           
    ]);

    $password = $request['password'];
    $passwordConfirm = $request['password_confirm'];

    if ($password !== $passwordConfirm) {
        return redirect()->back()->with(['fail' => 'password fields do not match!']);
    }

    $admin = new Admin();
    $admin->username = $request['username'];
    $admin->password = $request['password'];
    $admin->save();

    return redirect()->route('index');       

}

I have run php artisan migrate in composer, but I get the "nothing to migrate" response. I have tried refreshing the database via composer, but to no avail. The table 'admins' is showing up in phpmyadmin.

Update 1: Added full error message

like image 319
Frosty619 Avatar asked Mar 07 '16 18:03

Frosty619


2 Answers

The error comes from the validation part:

  $this->validate($request, [
    'username' => 'required|unique:admin|max:30|min:3',
    'password' => 'required|min:5',
    'password_confirm' => 'required'           
]);

When checking for uniqueness of the username you're referring to the admin table in the database, however the table is called admins (with a s at the end). Therefore Laravel is looking for a table called admin which of course doesn't exist. Add the s at the end and it should work.

like image 164
Maximilian Schwarzmüller Avatar answered Oct 26 '22 15:10

Maximilian Schwarzmüller


Try to add protected $table = 'admins'; to Admin model after trait.

like image 43
Alexey Mezenin Avatar answered Oct 26 '22 14:10

Alexey Mezenin