简体   繁体   中英

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

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

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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM