sqlstate[42s02]:找不到基表或视图:1146表“laravel\u abonamenty2.currences”不存在

fhg3lkii  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(271)

我犯了这个错误

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel_abonamenty2.currencies' doesn't exist (SQL: select `id`, `currency`, `course` from `currencies`)

这是我的控制器产生错误。我不知道拉威尔为什么要找货币表。我的表和迁移称为currenchis。

public function create()
{
    $users = User::all('showname', 'id');
    $forms = Form::all('id', 'form');
    $currencys = Currency::all('id', 'currency', 'course');
    return view('invoices.create')->with('users', $users, 'forms', 'currencys');
}

这是我的货币模型:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Currency extends Model
{
    protected $fillable = [
        'id', 'currency', 'course',
    ];

    public function invoice()
    {
        return $this->belongsTo('App\Invoice');
    }

    public function proform()
    {
        return $this->belongsTo('App\Proform');
    }
}

这是我的货币迁移

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class Currencys extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('currencys', function (Blueprint $table) {
            $table->increments('id');
            $table->string('currency')->nullable();
            $table->string('course')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('currencys');
    }
}
vptzau2j

vptzau2j1#

因为 currency 是货币而不是货币。laravel自动检测 snake case plural 模型的名称。文档
在模型中,可以指定另一个表名,如:

protected $table = 'currencys';

有了这个,拉威尔将搜索currencys表。

knsnq2tg

knsnq2tg2#

按照惯例,“snake case”这个类的复数名将用作表名,除非明确指定了另一个名称。https://laravel.com/docs/7.x/eloquent#eloquent-模型约定
在你的例子中,它将 Currency (型号名称)改为复数 currencies . 因此,如果需要自定义表名,
您需要在模型中指定表的名称。
货币模型

class Currency extends Model
{
  protected $table="currency";
...
}

相关问题