简体   繁体   English

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

[英]How can i make a flip-flop in laravel-php with boolean values requested from database row?

So i`m trying to check if my value in row 'Sent' is true or false then i want to create a flip flop which change a html value based on value in that row. 所以我正在尝试检查“已发送”行中的值是对还是错,然后我想创建一个触发器,根据该行的值更改html值。

I`ve tried to create a flip flop with 2 variables then i used if statement to compare between database and true/false. 我试图用2个变量创建一个触发器,然后使用if语句在数据库和true / false之间进行比较。

DB migration 数据库迁移

 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();
        });
    }

Controller.php Controller.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 (which i think its not necessary but i will put it here) Blade.php(我认为这不是必需的,但我将其放在此处)

<td>
 @if($qrcode->used === 1)
    <b><p name="Yes">Yes</p></b>
 @else
   <b><p>No</p></b>
 @endif
</td>

Routes and Model are well created...my problem is in controller! 路线和模型创建良好...我的问题出在控制器上!

Every time i try to change the value,code return always 1 even if the database row is false. 每次我尝试更改该值时,即使数据库行为false,代码也始终返回1。

The problem is get returns a collection. 问题是get返回一个集合。 You're trying to check against the value. 您正在尝试检查该值。

Try using the value method instead. 尝试改用value方法。 From the docs: 从文档:

If you don't even need an entire row, you may extract a single value from a record using the value method. 如果您甚至不需要整行,都可以使用value方法从记录中提取单个值。

https://laravel.com/docs/5.8/queries#retrieving-results https://laravel.com/docs/5.8/queries#retrieving-results

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM