php 未找到基表或视图:1146 表 Laravel 5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30159257/
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-08-25 21:35:21  来源:igfitidea点击:

Base table or view not found: 1146 Table Laravel 5

phpmysqllaravel

提问by ray sn0w

Im getting this error when I try to save data to mysql using Laravel 5, other forms and save() methods work fine but this one:

当我尝试使用 Laravel 5 将数据保存到 mysql 时出现此错误,其他形式和 save() 方法工作正常,但这个:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'sistemal5.cotizacions' doesn't exist (SQL: insert into `cotizacions` (`customer_id`, `total`, `updated_at`, `created_at`) values (1501, 150.69, 2015-05-11 03:16:25, 2015-05-11 03:16:25))

Here is my Controller store method:

这是我的控制器存储方法:

public function store(CotFormRequest $request)
    {    
        $quote = new Cotizacion;
        $quote->customer_id = Input::get('data.clientid');
        $quote->total = Input::get('data.totalAftertax');    
        $quote->save();    
    }

And here is my model:

这是我的模型:

<?php namespace App\Models\Cotizacion;

use Illuminate\Database\Eloquent\Model;


class Cotizacion extends Model {

}

I must be overlooking something really obvious cause i cant understand why Laravel is adding an "S" the table is cotizacion not cotizacions.

我一定忽略了一些非常明显的东西,因为我无法理解为什么 Laravel 在表中添加一个“S”是 cotizacion 而不是 cotizacions。

How can i troubleshoot this?

我该如何解决这个问题?

回答by James Spence

I'm guessing Laravel can't determine the plural form of the word you used for your table name.

我猜 Laravel 无法确定您用于表名的单词的复数形式。

Just specify your table in the model as such:

只需在模型中指定您的表:

class Cotizacion extends Model{
    public $table = "cotizacion";

回答by Inamur Rahman

Check your migration file, maybe you are using Schema::table, like this:

检查您的迁移文件,也许您正在使用 Schema::table,如下所示:

Schema::table('table_name', function ($table)  {
    // ...
});

If you want to create a new table you must use Schema::create:

如果要创建新表,必须使用 Schema::create:

Schema::create('table_name', function ($table)  {
    // ...
});

回答by sao

I faced this problem too in laravel 5.2 and if declaring the table name doesn't work,it is probably because you have some wrong declaration or mistake in validation code in Request (If you are using one)

我在 Laravel 5.2 中也遇到过这个问题,如果声明表名不起作用,可能是因为您在请求中的验证代码中有一些错误的声明或错误(如果您正在使用)

回答by panjeh

If your error is not related to the issue of

如果您的错误与问题无关

Laravel can't determine the plural form of the word you used for your table name.

Laravel 无法确定您用于表名的单词的复数形式。

with this solution

使用此解决方案

and still have this error, try my approach. you should find the problem in the default "AppServiceProvider.php"or other ServiceProviders defined for that application specifically or even in Kernel.phpin App\Console

仍然有这个错误,试试我的方法。您应该在默认的“AppServiceProvider.php”或专门为该应用程序定义的其他服务提供者中找到问题,甚至在 App\Console中的Kernel.php

This error happened for me and I solved it temporary and still couldn't figure out the exact origin and description.

这个错误发生在我身上,我暂时解决了它,但仍然无法弄清楚确切的起源和描述。

In my case the main problem for causing my table unable to migrate, is that I have running code/query on my "PermissionsServiceProvider.php"in the boot() method.

在我的情况下,导致我的表无法迁移的主要问题是我在 boot() 方法中的“PermissionsServiceProvider.php”上运行了代码/查询。

In the same way, maybe, you defined something in boot() method of AppServiceProvider.phpor in the Kernel.php

以同样的方式,也许你在AppServiceProvider.php 的boot() 方法或 Kernel.php 中定义了一些东西

So first check your Serviceproviders and disable code for a while, and run php artisan migrate and then undo changes in your code.

因此,首先检查您的 Serviceproviders 并禁用代码一段时间,然后运行 ​​php artisan migrate,然后撤消代码中的更改。

回答by samad.naghipour

I also had this problem when doing migration => after performing php artisan migrate:refresh --seed command

在执行 php artisan migrate:refresh --seed 命令后,我在做迁移时也遇到了这个问题 =>

"Base table or view not found: 1146 Table posts do not exist"and I solved it, I was using "soft deletes" in a separate migration file, and my original migration for "posts" table which was responsible for creating the "posts" table was performing after the "soft deletes" migration in the database\migration folder, I simply needed to rename the "posts" migration file name and put it before "softdeletes" migration to perform sooner. the do the migration again => php artisan migrate:refresh --seed it's done.

“未找到基表或视图:1146 个表帖子不存在”,我解决了它,我在单独的迁移文件中使用了“软删除”,而我对“帖子”表的原始迁移负责创建“帖子” " 表在 database\migration 文件夹中的“软删除”迁移之后执行,我只需要重命名“posts”迁移文件名并将其放在“softdeletes”迁移之前以更快地执行。再次进行迁移 => php artisan migrate:refresh --seed 就完成了。

回答by Hadi

When you are passing the custom request in the controller method, and your request file doesn't follow the proper syntax or table name is changed, then laravel through this type of exception. I'll show you in Example.

当您在控制器方法中传递自定义请求,并且您的请求文件没有遵循正确的语法或表名被更改时,laravel 就会通过这种类型的异常。我将在示例中向您展示。

My Request code.
     public function rules()
        {
            return [
                'name' => 'required|unique:posts|max:50',
                'description' => 'required',
            ];
        }

But my table name is todos not posts so that's why it through "Base table or view not found: 1146 Table Laravel 7


    I forgot to change the table name in first index.
     public function rules()
        {
            return [
                'name' => 'required|unique:todos|max:50',
                'description' => 'required',
            ];
        }

回答by Abhi

Just run the command:

只需运行以下命令:

php artisan migrate:refresh --seed