所以我试图检查我在“发送”行中的值是真还是假然后我想创建一个触发器,它根据该行中的值更改 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(); }); }控制器.phppublic 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。
1 回答
MMMHUHU
TA贡献1834条经验 获得超8个赞
问题是get
返回一个集合。您正在尝试检查该值。
尝试改用该value
方法。从文档:
如果您甚至不需要整行,您可以使用 value 方法从记录中提取单个值。
https://laravel.com/docs/5.8/queries#retrieving-results
$sent = DB::table('qrcode')->where('id', $id)->value('sent');
- 1 回答
- 0 关注
- 135 浏览
添加回答
举报
0/150
提交
取消