朋友 我的 php 代码有问题,我试图使用 php、html 和 laravel 将数据保存到 mySQL 数据库中,我使用该函数创建此数据并将其保存到 MySQL 数据库中,但每次我单击时都不起作用保存数据的按钮视图返回但数据未保存?有谁可以帮助我吗,
我的路线代码:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ServicePaieController;
route::get('/',[ServicePaieController::class, 'index']);
Route::match(['get', 'post'], '/search', [ServicePaieController::class, 'searchDataAbsence'])->name('searchDataAbsence');
Route::post('/', [ServicePaieController::class,'storeAbsence'])->name('storeAbsence');
Route::middleware([
'auth:sanctum',
config('jetstream.auth_session'),
'verified',
])->group(function () {
Route::get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
});
我的控制器代码中的功能代码:
public function storeAbsence(Request $request) {
$request->validate([
'Matricule' => 'required',
'Structure' => 'required',
'NomPrenom' => 'required',
'Abs_Periode' => 'required',
'Abs_Mois' => 'required',
'Abs_Exercice' => 'required',
'Abs_jour_sp' => 'required',
'Abs_Total_ap' => 'required',
'Abs_Retenue_sp' => 'required',
'Abs_jour_aes' => 'required',
'Abs_Total_aes' => 'required',
'Abs_Retenue_aes' => 'required',
]);
Log::info('Your log message here');
Absence::create([
'Matricule' => $request->search,
'Structure' => $request->nomStr,
'NomPrenom' => $request->nomPer,
'Abs_Periode' => $request->searchPeriode,
'Abs_Mois' => "Décembre",
'Abs_Exercice' => $request->searchExercice,
'Abs_jour_sp' => $request->jour_sp,
'Abs_Total_ap' => $request->total_sp,
'Abs_Retenue_sp' => $request->retenue_sp,
'Abs_jour_aes' => $request->jour_aes,
'Abs_Total_aes' => $request->total_aes,
'Abs_Retenue_aes' => $request->retenue_aes,
]);
return redirect()->route('service_paie.service_paie_module')->with('success', "Absence data created successfully");
}
HTML 代码:
<form class="font_size" id="addAbsenceForm" action="{{ route('storeAbsence') }}" method="post">
@csrf
<!-- your form fields go here -->
<div class="div_design">
<div class="btn_design" role="group" aria-label="Actions">
<input id="addAbsenceButton" class="btn_designBtn btn btn-primary" type="submit" value="Enregistrer">
</div>
</div>
</form>
在“商店缺席”中你会
Request->validate(…)
发生的情况是它失败了,因为并非所有必填字段都在那里。验证检测到并执行
Redirect->back()
因此,将所有必填字段添加到您的表单中,显示错误以检查错误,您的问题就得到解决
错误显示:
@error(‘inputname’)
<div class="alert alert-danger">{{ $message }}</div>
@enderror