laravel QueryException SQLSTATE[42S02]: 基表或视图未找到:1146 表

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

QueryException SQLSTATE[42S02]: Base table or view not found: 1146 Table

laraveleloquentlaravel-5.4

提问by Atmojo211

i learn foreign key in laravel for the first time .. i have this message error when i insert data on the form to table . i think my table is fine. can u tell me how to solve this ?

我第一次在 Laravel 中学习外键.. 当我将表单上的数据插入到表中时,出现此消息错误。我觉得我的桌子很好。你能告诉我如何解决这个问题吗?

Controller :

控制器 :

public function tambah(Request $request){
    $a = new Admin;
    $this->validate($request, ['nama'=>'required|unique:ab']);
    $a->id = $request->id;
    $a->nama = $request->nama;
    $a->save();

    return redirect()->to('admin/data_desa')->with('success','Data berhasil ditambahkan');
}


}

table :

桌子 :

 class CreateDesasTable extends Migration
 {
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    //
    Schema::dropIfExist('desas');
    Schema::create('desas', function(Blueprint $table){
    $table->increments('id');
    $table->string('nama');

});
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    //
    Schema::dropIfExist('desas');

}
}

Route :

路线 :

Route::group(['middleware' => 'web'], function(){
Route::group(['prefix' => 'tanitani'], function(){
    Route::get('admin', 'AdminController@index');
    Route::get('admin/data_desa','AdminController@tampil');
    Route::post('admin/data_desa', 'AdminController@tambah');

回答by Maraboc

You have tou override the table name ih the Adminmodel, since you have a different table name.

您必须覆盖Admin模型中的表名,因为您有不同的表名。

Why ?

为什么 ?

Because by convetion Laravel will assume that the table name is admins.

因为按照约定 Laravel 会假设表名是admins.

So add this line to your model :

因此,将此行添加到您的模型中:

protected $table = 'desas';

回答by Juan Caicedo

It looks like you are not working with laravel eloquent model conventions.

看起来您没有使用 laravel eloquent 模型约定。

Are you sure that model Adminis the model that you are required to save desasdata in your controller?

您确定该模型Admin是您需要desas在控制器中保存数据的模型吗?

if the answer is true update model Admin with

如果答案是真的更新模型管理员

protected $table = 'desas';

Recommendation: Try to use the same name convention between models and database tables.

建议:尽量在模型和数据库表之间使用相同的命名约定。

See https://laravel.com/docs/5.4/eloquent#eloquent-model-conventionsfor more information.

有关更多信息,请参阅https://laravel.com/docs/5.4/eloquent#eloquent-model-conventions

Recommended docs before continue:

继续之前推荐的文档:

回答by ibrahim Mohamed

I just had this issue, simply check the migration file, the name of the table needs to be plural when I search:

刚遇到这个问题,简单查看迁移文件,搜索时表名需要为复数:

App\post::all();
Table 'laravel.posts' doesn't exist (SQL: select * * from `posts`)'

so change create it all again make:migration create_posts_table run the command and migrate. then:

所以更改创建它再次 make:migration create_posts_table 运行命令并迁移。然后:

App\posts::all();

It worked for me.

它对我有用。