SQLSTATE [42S02]:未找到基表或视图:1146 表 'hr.staff' 不存在

Posted

技术标签:

【中文标题】SQLSTATE [42S02]:未找到基表或视图:1146 表 \'hr.staff\' 不存在【英文标题】:SQLSTATE[42S02]: Base table or view not found: 1146 Table 'hr.staff' doesn't existSQLSTATE [42S02]:未找到基表或视图:1146 表 'hr.staff' 不存在 【发布时间】:2021-12-22 17:18:50 【问题描述】:

大家好!我想做 CRUD,但尝试提交表单时出错。错误显示,“SQLSTATE[42S02]: Base table or view not found: 1146 Table 'hr.staff' 不存在”。下面显示了我的数据库结构和我的编码。

员工模型:-

    class Staffs extends Model

    use HasFactory;

    protected $fillable = [
        'name', 'staffid', 'address', 'religion', 'email', 'phonenum', 'maritalstatus'
    ];

人员迁移表:-

public function up()

    Schema::create('staffs', function (Blueprint $table) 
        $table->id();
        $table->timestamps();
        $table->string('name');
        $table->integer('staffid');
        $table->string('address');
        $table->string('religion');
        $table->string('email')->unique();
        $table->integer('phonenum');
        $table->string('maritalstatus');
    );

StaffContoller:-

public function store(Request $request)

    $request->validate([
        'name' => 'required',
        'staffid' => 'required',
        'address' => 'required',
        'religion' => 'required',
        'email' => 'required',
        'phonenum' => 'required',
        'maritalstatus' => 'required',
    ]);

    Staff::create($request->all());
 
    return redirect()->route('staffs.index')
                    ->with('success','Staff data has been created successfully.');

addstaff.blade.php:-

<form method="POST" action=" route('staffs.store') ">
                    @csrf
                    <div class="grid grid-cols-2 gap-6">
                        <div class="grid grid-rows-2 gap-6">
                            <div>
                                <x-label for="name" :value="__('Name:')" />
                                <x-input id="name" class="block mt-1 w-full" type="text" name="name" value=" old('name') " autofocus />
                            </div>

                            <div>
                                <x-label for="staffid" :value="__('Staff ID:')" />
                                <x-input id="staffid" class="block mt-1 w-full" type="integer" name="staffid" value=" old('staffid') " autofocus />
                            </div>

<div class="flex items-center justify-end mt-4">
                            <x-button class="ml-3">
                                 __('Submit') 
                            </x-button>
                        </div>

P/s:无法粘贴完整的提交表单,但所有字段都在那里。

数据库结构:-

【问题讨论】:

【参考方案1】:

您的表名是staffs 而不是staff

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'hr.staff' doesn't exist

将模型的$table 属性更新为正确的表名,然后您应该能够使用此模型创建记录。 Staff::create($request-&gt;all());

【讨论】:

已经重命名模型名称并收到此错误,“SQLSTATE[22003]: Numeric value out of range: 1264 Out of range value for column 'phonenum' at row 1” @Daisy 发布您的 Staff 模型和迁移。 完成更新问题 @Daisy 'XL212863' 不是整数。在迁移中将 $table-&gt;integer('staffid'); 更改为 $table-&gt;string('staffid'); @Daisy 您是否运行php artisan migrate:refresh 以使用新列定义重建表?

以上是关于SQLSTATE [42S02]:未找到基表或视图:1146 表 'hr.staff' 不存在的主要内容,如果未能解决你的问题,请参考以下文章

SQLSTATE [42S02]:未找到基表或视图:1146 表 'pdone.BaseUser' 不存在

SQLSTATE [42S02]:未找到基表或视图:1146 表 'laravel_abonamenty2.currencies' 不存在

SQLSTATE [42S02]:未找到基表或视图:1146表'softwareproject.o_r_f_o_l_s'在laravel中不存在错误

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

未找到基表或视图:1146 表“epharmacy.medicines”不存在

创建类别并重定向到 category.index 后,在 laravel 中找不到基表或视图