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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35851563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:22:02  来源:igfitidea点击:

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

phplaravel

提问by Frosty619

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 thispost, but even after I implemented the solution(shown below), I keep getting the same error.

我知道错误是错误日志中出现的名称与其他地方的定义方式不匹配(在数据库文件夹中,但我无法解决问题。我四处搜索并找到了这篇文章,但即使在我之后实施了解决方案(如下所示),我不断收到相同的错误。

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

我正在使用 Laravel 5.2。我的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 :

管理模型如下所示:

<?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 postRegistermethod

这是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 migratein 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.

我已经php artisan migrate在作曲家中运行,但我得到了“没有什么可迁移”的响应。我曾尝试通过作曲家刷新数据库,但无济于事。表'admins' 显示在phpmyadmin 中。

Update 1:Added full error message

更新 1:添加了完整的错误消息

回答by Maximilian Schwarzmüller

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 usernameyou're referring to the admintable in the database, however the table is called admins(with a sat the end). Therefore Laravel is looking for a table called adminwhich of course doesn't exist. Add the sat the end and it should work.

在检查username您指的admin是数据库中的表的唯一性时,但是该表被调用admins(以s结尾)。因此 Laravel 正在寻找一个名为adminwhich 当然不存在的表。在最后添加s,它应该可以工作。

回答by Alexey Mezenin

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

尝试protected $table = 'admins';在 trait 之后添加到 Admin 模型。

回答by VineFreeman

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.

不确定是否相关,但我收到了该消息,因为我的模型与 Db 表的名称不同。如果不是模型的复数,您可以在模型中添加受保护的表名称。

回答by iboinas

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::tablemethod.

要进行更改,请使用Schema::table方法。