Laravel 8多提交按钮仅store生效,storeAns与storeTot无法存储数据求助
Laravel 8 storeAns和storeTot方法无法存储数据排查修复
我在Laravel 8页面中设置了多个用于向数据库插入数据的提交按钮,对应控制器的store、storeAns、storeTot方法。目前仅store方法能正常存储数据,storeAns和storeTot无法完成数据存储,Laravel日志中无报错信息,但通过dd($request->all())可以看到请求数据。以下是我的代码示例:
控制器代码
public function store(Request $request) { $days = $request->hari; $hours = $request->jam; $minutes = $request->menit; $model = new Q_Topic_User; $model->status = $request->status; $model->topic_id = $request->topic_id; $model->timer = Carbon::now()->addDays($days)->addHours($hours)->addMinute($minutes); $topik = $request->topic_id; $model->user_create = auth()->id(); $model->save(); return redirect('qanswers/'. $topik); } public function storeAns(Request $request) { $modellama = Q_Answer::where('user_create', '=', $request->kode_user) ->Where('topic_id', '=', $request->topic_id) ->Where('question_id', '=', $request->question_id); $a = $request->user_answer; $b = $request->answer; if ($a === $b){ $nilai = "1"; } else{ $nilai = "0"; } if ($modellama >= "1"){ $modellama->delete(); } $model = new Q_Answer; $model->topic_id = $request->topic_id; $model->question_id = $request->question_id; $model->user_answer = $request->user_answer; $model->answer = $request->answer; $model->hasil = $nilai; $model->user_create = auth()->id(); $topik = $request->topic_id; $model->save(); return redirect('qanswers/'. $topik); } public function storeTot(Request $request) { $tot_nilai = Q_Answer::select(DB::raw("CAST(SUM(hasil) as int) as tot_nilai")) ->Where('topic_id', '=', $request->topic_idi) ->Where('user_create', '=', auth()->id()) ->pluck('tot_nilai'); $tot_soal = Q_Question::select(DB::raw("CAST(COUNT(topic_id) as int) as tot_soal")) ->Where('topic_id', '=', $request->topic_idi) ->pluck('tot_soal'); dd($request->all()); $model = new Q_Answer_Tot; $model->topic_id = $request->topic_idi; $model->totalsoal = $tot_soal; $model->totalnilai = $tot_nilai; $model->user_create = auth()->id(); $model->save(); $topik = $request->topic_id; return redirect('qanswers/'. $topik); }
Blade视图代码
<form method="POST" action="{{ url('qanswers') }}" enctype="multipart/form-data"> @csrf @foreach ($datas as $d_value) <button type="submit" class="btn btn-sm bg-success">Kirim data</button> @endforeach </form> <form method="POST" action="{{ url('qanswers.storeAns') }}" enctype="multipart/form-data"> @csrf @foreach ($questionss as $q_value) <button type="submit" class="btn btn-sm bg-success">Kirim Pilihan Pertanyaan</button> @endforeach </form> <form method="POST" action="{{ url('qanswers.storeTot') }}" enctype="multipart/form-data"> @csrf @foreach ($questionss as $t_value) <button type="submit" class="btn btn-sm bg-success">Kirim Pilihan Peserta</button> @endforeach </form>
路由代码(web.php)
Route::resource('/qanswers', Q_AnswersController::class); Route::post('/storeAns',[Q_AnswersController::class, 'storeAns'])->name('storeAns'); Route::post('/storeTot',[Q_AnswersController::class, 'storeTot'])->name('storeTot');
问题排查与修复
1. 视图表单Action路径错误
视图中storeAns和storeTot对应的表单action使用了错误的生成方式:
{{ url('qanswers.storeAns') }} {{ url('qanswers.storeTot') }}
url()方法会将传入的字符串直接拼接成URL,导致实际请求路径为/qanswers.storeAns,无法匹配到路由中定义的storeAns和storeTot路由。
修复方案:使用路由名称生成正确的action路径,路由已通过->name()命名,改为:
<form method="POST" action="{{ route('storeAns') }}" enctype="multipart/form-data"> <form method="POST" action="{{ route('storeTot') }}" enctype="multipart/form-data">
2. storeAns方法中查询判断逻辑错误
$modellama是Eloquent查询构造器实例,直接和字符串"1"比较没有意义,无法判断是否存在匹配数据:
if ($modellama >= "1"){ $modellama->delete(); }
修复方案:使用exists()方法判断是否存在匹配记录:
$modellama = Q_Answer::where('user_create', '=', $request->kode_user) ->where('topic_id', '=', $request->topic_id) ->where('question_id', '=', $request->question_id); if ($modellama->exists()) { $modellama->delete(); }
3. storeTot方法中未执行save操作
storeTot方法里$model->save()之前有dd($request->all());,该方法会中断程序执行,导致后续的保存和跳转代码永远不会运行。
修复方案:移除或注释掉该dd()语句,让代码执行到保存逻辑。
4. storeTot方法中数据获取与跳转参数错误
pluck('tot_nilai')返回的是Collection集合,不是单个数值,直接赋值给模型字段会导致存储错误,应改用value()获取单个值:$tot_nilai = Q_Answer::select(DB::raw("CAST(SUM(hasil) as int) as tot_nilai")) ->where('topic_id', '=', $request->topic_idi) ->where('user_create', '=', auth()->id()) ->value('tot_nilai'); $tot_soal = Q_Question::select(DB::raw("CAST(COUNT(topic_id) as int) as tot_soal")) ->where('topic_id', '=', $request->topic_idi) ->value('tot_soal');- 跳转使用的
$topik = $request->topic_id;,但请求参数是topic_idi,会导致跳转路径错误,改为:$topik = $request->topic_idi; return redirect('qanswers/'. $topik);
内容的提问来源于stack exchange,提问作者indahwati indahwati
相关产品推荐
相关产品推荐

