在 laravel 的弹出模式中更新数据
Posted
技术标签:
【中文标题】在 laravel 的弹出模式中更新数据【英文标题】:Update data in popup modal in laravel 【发布时间】:2021-09-08 21:36:31 【问题描述】:我需要从 laravel 中的弹出模式更新用户数据,但我做不到。数据库中的一切都保持不变。
我还是 laravel 的新手,而且我的截止日期很紧。这真让我抓狂。我知道我可能只是在做一些愚蠢的事情,但我很难知道它是什么。
当我点击提交按钮更新数据时,没有任何反应。
web.php
/* TEACHERS MATTERS*/
Route::get('timetable/user_id', [TimetableController::class, 'create'])->name('timetable.up');
Route::post('timetable/user_id', [TimetableController::class, 'store']);
Route::get('timetable', [TimetableController::class, 'create']);
Route::post('timetable', [TimetableController::class, 'store']);
Route::Post('timetable/update', [TimetableController::class, 'update'])->name('timetable/update');
在上面,这是路线:
Route::Post('timetable/update', [TimetableController::class, 'update'])->name('timetable/update');
时间表控制器:
public function update(Request $request)
$timetable = Timetable::find($request->get('id'));
$timetable['tea_id'] = $request->get('tea_id');
$timetable['subject'] = $request->get('subject');
$timetable['class'] = $request->get('class');
$timetable['day'] = $request->get('day');
$timetable['start_time'] = $request->get('start_time');
$timetable['close_time'] = $request->get('close_time');
$update = $timetable->save();
if($update)
return redirect()->back()->with('success', 'New Schedule Created');
else
return redirect()->back()->with('failed', 'Update failed');
public function status($id, $status)
// echo $status;
// dd($id);
$timetable = Timetable::find($id);
$timetable['status'] = $status;
$update = $timetable->save();
if($update)
return redirect()->back()->with('success', 'Status changed');
else
return redirect()->back()->with('failed', 'Action not performed');
这是数据库图像:
enter image description here
timeblade.php
<!-- Start of modal update -->
<div class="modal fade" id="update" tabindex="-1" role="dialog" aria-
labelledby="demoModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="demoModalLabel">Change day </h5>
<button type="button" class="close" data-dismiss="modal" aria-
label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action=" route('timetable/update') " method="post">
<center>
@csrf
<div style="width: 90%; ">
<input type="text" class="form-control" name="day"
id="day" aria-describedby="basic-addon1"
style="border-radius: 5px;">
</div>
</center>
<div class="modal-footer">
<button type="button" class="btn btn-outline-danger" data-dismiss="modal"
aria-label="Close">Close</button>
<button type="submit" class="btn btn-outline-success">Save
changes</button>
</div>
</form>
</div>
</div>
</div>
<!-- End of modal update -->
【问题讨论】:
【参考方案1】:您在 Eloquent 模型中使用了数组语法而不是对象语法。 见https://laravel.com/docs/8.x/eloquent#updates
更正:
public function update(Request $request)
$timetable = Timetable::find($request->get('id'));
$timetable->tea_id = $request->get('tea_id');
$timetable->subject = $request->get('subject');
$timetable->class = $request->get('class');
$timetable->day = $request->get('day');
$timetable->start_time = $request->get('start_time');
$timetable->close_time = $request->get('close_time');
$update = $timetable->save();
if($update)
return redirect()->back()->with('success', 'New Schedule Created');
else
return redirect()->back()->with('failed', 'Update failed');
这个 sn-p 缺少验证和一些检查(例如,您应该检查您的模型是否存在于数据库中)
【讨论】:
以上是关于在 laravel 的弹出模式中更新数据的主要内容,如果未能解决你的问题,请参考以下文章
如何禁用显示 Adobe Flash Player 未更新的弹出窗口?
使用 axios.put [laravel + vuejs 3] 更新模式内的数据时出现错误 422
RecyclerView Item Android Studio的弹出菜单功能