如何在Laravel中积极和非活动的布尔值

发布于 2025-01-23 13:22:18 字数 1450 浏览 0 评论 0 原文

每当单击我的按钮时,我想知道如何在数据库中制作布尔数据。 我已经可以在单击按钮时可以执行真实,我想知道如何使其虚假。希望我能得到答案,谢谢!顺便说一句,这是我第一次在此处发布问题,而源代码来自代码,Stein yt a a a a the Cread。 这是我使用的一些代码。

我的更新表格的代码

<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' : ''
            
        ])
        >

ui 的屏幕快照

I want to know how to make a Boolean data true or false in database whenever my button is clicked.
I already can perform a true when button is clicked, I want to know how to make it false. I hope I can get a answer thank you! BTW, this is my first time to post a question here and the source code are from Code With Stein from YT a big credits.
Here are some of codes I used.

My code for my update form

<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>

My code for route

Route::patch('/{todo}', [TodoController::class, 'update']);

My code for my controller

  public function update(Todo $todo) {
    $todo->update(['isDone' => true]);
    return redirect('/')->with('msg1', 'Marked as done!');;

}    

UI change when I the button clicked

        <div 
        @class([
            'py-4 flex items-center border-b border-gray-300 px-3',
            $todo->isDone ? 'bg-green-200' : ''
            
        ])
        >

Screenshot of UI

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨后咖啡店 2025-01-30 13:22:18

如果要将其变为false(或类似 toggle ),可以尝试将值与

类似的东西

// "$todo->isDone" value is false
$todo = Todo::create(['isDone' => false]);

// Since ! means "not" in logical operator, so !false = true
$todo->update(['isDone' => !$todo->isDone]);

交换,我在此处测试过

https://web.tinkerwell.app/#/snippets/dc8a7b9f-59a6-4c07-84ca- 1BB2FC8C1DA4

If you want to make it false (or like toggle), can try swap the value with !

something like this

// "$todo->isDone" value is false
$todo = Todo::create(['isDone' => false]);

// Since ! means "not" in logical operator, so !false = true
$todo->update(['isDone' => !$todo->isDone]);

I've tested here

https://web.tinkerwell.app/#/snippets/dc8a7b9f-59a6-4c07-84ca-1bb2fc8c1da4

明媚殇 2025-01-30 13:22:18

只需用这个控制器替换

public function update(Todo $todo) {
 $todo->update(['isDone' => !$todo->isDone]);
 return redirect('/')->with('msg1', 'Marked as done!');;
} 

just replace with this Controller

public function update(Todo $todo) {
 $todo->update(['isDone' => !$todo->isDone]);
 return redirect('/')->with('msg1', 'Marked as done!');;
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文