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

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

大家好!我想制作 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'
    ];
}

员工迁移table:-

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

职员管理员:-

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: 无法粘贴提交的完整表格,但所有字段都在那里。

数据库结构:-

你的 table 名字是 staffs 而不是 staff

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

将模型的 $table 属性 更新为正确的 table 名称,然后您应该可以使用该模型创建记录。 Staff::create($request->all());