我正在嘗試更新狀態。如果狀態為 0,則單擊時必須將其更新為 1。但如果單擊時狀態為 1,則必須將其更新為 0。
我的控制器
public function status(Course $course)
{
if ($course->status == 0) {
$course->update(['status' => '1']);
return redirect('/profile')->with('statusOn', 'status on!');
} else {
$course->update(['status' => '0']);
return redirect('/profile')->with('statusOff', 'status off!');
}
}
路線
Route::post('/course/status/{course}', [CourseManagementController::class, 'status']);
刀刃
<form method="POST" action="/course/status/{{$c->id}}">
@csrf
@if($c->status == 0)
<button type="submit" class="btn btn-success">active</button>
@else
<button type="submit" class="btn btn-danger">deactive</button>
@endif
</form>
uj5u.com熱心網友回復:
您好,請除錯!
public function status(Course $course)
{
dump($course); // the data is okay or not
if ($course->status == 0) {
$status = $course->update(['status' => '1']);
dd($status); // check the update status
// return redirect('/profile')->with('statusOn', 'status on!');
} else {
$status = $course->update(['status' => '0']);
dd($status); // check the update status
// return redirect('/profile')->with('statusOff', 'status off!');
}
}
如果一切正常,請檢查fillable您Course班級的變數,status可能在fillable陣列中丟失
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/404699.html
標籤:
上一篇:如何使影像的大小與導航按鈕相同?
下一篇:移動引導程式中的導航欄不起作用
