7

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

Community
  • 1
  • 1
Frosty619
  • 1,381
  • 4
  • 23
  • 33

4 Answers4

19

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.

  • @Frosty619 I too believe this to be the case. change `unique:admin` to `unique:admins` and let us know if this works. – samuraiseoul Mar 08 '16 at 08:07
  • Hi @Maximilian Schwarzmüller You are a life saver, I read all repeating solutions but this line just saved my life: `error comes from the validation part`. Wow – fWd82 Sep 05 '20 at 21:00
3

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

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • 1
    I am not sure what you mean by "after trait"? I did add the line you suggested after 'use Authenticatable;' in my Admin model, but I get the same error. – Frosty619 Mar 07 '16 at 19:03
2

Not sure if relevant but I got that message because my model was a different name to Db table. You can add a protected table name in model if it isn't the plural of the model.

VineFreeman
  • 715
  • 7
  • 12
  • 1
    Add the protected table name in the model class. Like mentioned below: protected $table = 'admins'; at the top of your admin model – VineFreeman Mar 10 '16 at 22:18
1

Laravel is telling you it cannot find such table.

dd() is your friend.

$admin = new Admin();
dd($admin);

Check what's the table it has on properties, and it will point you in the right way.

Assuming your DB config is properly setup??

Also, when you migrate a DB, and it runs a class, it stores the step in the migrations table.

So, if you want to migrate something more, the point is you make another migration.

To make changes, use the Schema::table method.

ijpatricio
  • 170
  • 7