猿问

如何使用从数据库行请求的布尔值在 laravel-php 中制作触发器?

所以我试图检查我在“发送”行中的值是真还是假然后我想创建一个触发器,它根据该行中的值更改 html 值。


我尝试创建一个带有 2 个变量的触发器,然后我使用 if 语句在数据库和真/假之间进行比较。


数据库迁移


 public function up()

    {

        Schema::create('QrCode', function (Blueprint $table) {

            $table->bigIncrements('id');

            $table->string('codes',255);

            $table->timestamp('created_at');

            $table->boolean("sent")->default(0);

            $table->timestamp("sent_at")->nullable();

            $table->boolean("used")->default(0);

            $table->timestamp("used_at")->nullable();

        });

    }

控制器.php


public function update($id)

    {

        $sent = DB::table('qrcode')->where('id', $id)->get(['sent']);

        if ($sent == false) {

            DB::table('qrcode')->where('id', $id)->update(array('sent' => 1, 'sent_at' => now()));

        }

        if ($sent == true){

            DB::table('qrcode')->where('id', $id)->update(array('sent' => 0, 'sent_at' => now()));

        }

        return back();

    }

Blade.php(我认为没有必要,但我会把它放在这里)


<td>

 @if($qrcode->used === 1)

    <b><p name="Yes">Yes</p></b>

 @else

   <b><p>No</p></b>

 @endif

</td>

路由和模型创建得很好……我的问题出在控制器上!


每次我尝试更改值时,即使数据库行为假,代码也始终返回 1。


婷婷同学_
浏览 136回答 1
1回答

MMMHUHU

问题是get返回一个集合。您正在尝试检查该值。尝试改用该value方法。从文档:如果您甚至不需要整行,您可以使用 value 方法从记录中提取单个值。https://laravel.com/docs/5.8/queries#retrieving-results$sent = DB::table('qrcode')->where('id', $id)->value('sent');
随时随地看视频慕课网APP
我要回答