thinkphp6中thinkcollection::value()方法使用报 undefined错误
在研究 thinkphp6 官方手册时,遇到如下提示:
// 返回某个字段的值 db::table('think_user')->where('id', 1)->value('name');
然而,在实际使用中,却遇到了 call to undefined method thinkcollection::value() 错误。
原因
立即学习“PHP免费学习笔记(深入)”;
错误表明 value() 方法在 thinkcollection 类中未定义。这是因为,当前代码是在调用一个 select() 操作的结果,而 select() 操作返回的是一个集合对象,而不是一个数据行。
解决方案
要直接获取某个字段的值,只需要调用 where() 操作即可,无需再调用 select() 操作。修改后的代码如下:
$result = Db::table('admin_info')->where('id', '2')->value('name');
以上就是ThinkPHP6中使用 `think\Collection::value()` 方法报错该如何解决?的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号