我想知道每次單擊按鈕時如何在資料庫中使布爾資料為真或假。單擊按鈕時我已經可以執行 true ,我想知道如何使其為 false 。希望能得到答復謝謝!順便說一句,這是我第一次在這里發布問題,源代碼來自 YT 的Code With Stein ,非常感謝。這是我使用的一些代碼。
我的更新表單代碼
<form method="POST" action="/{{ $todo->id }}">
@csrf
@method('PATCH')
<button class="py-2 px-2 bg-green-500 text-white rounded-xl" id="show">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M5 13l4 4L19 7" />
</svg>
</button>
</form>
我的路線代碼
Route::patch('/{todo}', [TodoController::class, 'update']);
我的控制器代碼
public function update(Todo $todo) {
$todo->update(['isDone' => true]);
return redirect('/')->with('msg1', 'Marked as done!');;
}
當我單擊按鈕時 UI 發生變化
<div
@class([
'py-4 flex items-center border-b border-gray-300 px-3',
$todo->isDone ? 'bg-green-200' : ''
])
>
界面截圖
uj5u.com熱心網友回復:
如果您想將其設為 false (或類似toggle),可以嘗試將值與!
我在這里測驗過
https://web.tinkerwell.app/#/snippets/dc8a7b9f-59a6-4c07-84ca-1bb2fc8c1da4
uj5u.com熱心網友回復:
只需用這個控制器替換
public function update(Todo $todo) {
$todo->update(['isDone' => !$todo->isDone]);
return redirect('/')->with('msg1', 'Marked as done!');;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/462121.html
下一篇:如何在PHP中識別要處理的表單
