gpt4 book ai didi

php - 你如何为 Laravel 中是否存在记录设置 bool 值?

转载 作者:行者123 更新时间:2023-11-29 04:16:32 26 4
gpt4 key购买 nike

我正在尝试根据表中是否存在记录来设置 bool 值 $bln

这是我一直在尝试的:

$bln = (bool) DB::table('accounts')->where('name', $name)->pluck('id');

由于某些原因,$bln 似乎总是设置为 true。我做错了什么?

最佳答案

要实现这一点,您可以使用 count() 方法:

$bln = DB::table('accounts')->where('name', $name)->count() > 0;

如果稍后需要该对象,请加载它并使用 is_null() 进行检查:

$object = DB::table('accounts')->where('name', $name)->first();
$bln = !is_null($object);

关于php - 你如何为 Laravel 中是否存在记录设置 bool 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42811099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com