首页 > 解决方案 > SQLSTATE [42S02]:未找到基表或视图:1146 表“laravel_abonamenty2.currencies”不存在

问题描述

我有这个错误

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

这是我的控制器,它会产生错误。我不知道 Laravel 为什么要搜索货币表。我的表和迁移称为 Currencys。

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');
    }
}

标签: phpsqllaravelormframeworks

解决方案


因为of的复数currency货币而不是货币。Laravel 自动检测snake case plural模型名称的表名。文件

在您的模型中,您可以指定另一个表名,例如:

protected $table = 'currencys';

使用该 laravel 将搜索货币表。


推荐阅读