简体   繁体   English

Laravel错误:SQLSTATE [42S02]:找不到基表或视图

[英]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. 我正在使用Laravel 5.2。 I have an admins table in my database directory which looks like this: 我的数据库目录中有一个admins表,如下所示:

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 : Admin模型如下所示:

<?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 这是postRegister方法

 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. 我在作曲家中运行了php artisan migrate ,但是我得到了“无需迁移”的响应。 I have tried refreshing the database via composer, but to no avail. 我试过通过作曲家刷新数据库,但无济于事。 The table 'admins' is showing up in phpmyadmin. 表'admins'显示在phpmyadmin中。

Update 1: Added full error message 更新1:添加了完整的错误消息

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). 检查username唯一性时,您指的是数据库中的admin表,但该表称为admins (最后带有s )。 Therefore Laravel is looking for a table called admin which of course doesn't exist. 因此,Laravel正在寻找一个名为admin的表,当然不存在。 Add the s at the end and it should work. 最后添加s ,它应该工作。

Try to add protected $table = 'admins'; 尝试添加protected $table = 'admins'; to Admin model after trait. 特质后的Admin模型。

Not sure if relevant but I got that message because my model was a different name to Db table. 不确定是否相关,但我收到了该消息,因为我的模型与Db表的名称不同。 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. Laravel告诉你它找不到这样的桌子。

dd() is your friend. dd()是你的朋友。

$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. 要进行更改,请使用Schema::table方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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