大家好!我想制作 CRUD,但在嘗試提交表單時出現錯誤。錯誤顯示,“ SQLSTATE[42S02]: Base table or view not found: 1146 Table 'hr.staff' does not exist ”。下面顯示了我的資料庫結構和我的編碼。
員工模式:-
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');
});
}
員工主管:-
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" hljs-number">1 w-full" type="integer" name="staffid" value="{{ old('staffid') }}" autofocus />
</div>
<div hljs-number">4">
<x-button hljs-number">3">
{{ __('Submit') }}
</x-button>
</div>
P/s:無法粘貼完整的提交表單,但所有欄位都在那里。
資料庫結構:-
![SQLSTATE[42S02]:未找到基表或視圖:1146 表 'hr.staff' 不存在](https://img.uj5u.com/2021/11/12/8d497f47f8da4a9288b91f1093254ed3.png)
uj5u.com熱心網友回復:
您的表名staffs不是staff.
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'hr.staff' doesn't exist
將模型的$table屬性更新為正確的表名,然后您應該能夠使用此模型來創建記錄。Staff::create($request->all());
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/356354.html
