php - 像我们在使用 where in 子句的 sql 查询中所做的那样,在 laravel 中的 where 子句中使用一个查询结果的方法是什么?
问题描述
我在 Laravel 中写查询,但它给了我错误说
ErrorException:stdClass 类的对象无法转换为字符串
$subject_ids = DB::table('question_sets')
->select('subject_id')
->where('test_section_id','=',$testDetail->test_section_id)
->distinct()
->get();
$topic_ids = DB::table('topics')
->select('id')
->where('subject_id','=',$subject_ids)
->get();
解决方案
在下面的查询中
$subject_ids = DB::table('question_sets')
->select('subject_id')
->where('test_section_id','=',$testDetail->test_section_id)
->distinct()->get();
你得到一个集合,如果你想要一个特定的值,你可以使用first()
然后你可以做
$subject_id = DB::table('question_sets')
->select('subject_id')
->where('test_section_id','=',$testDetail->test_section_id)
->distinct()
->pluck('name')
->first();
和
$topic_ids = DB::table('topics')
->select('id')
->where('subject_id','=',$subject_id)
->get();
或者如果你想匹配所有 $subject_ids,你应该使用toArray()
和whereIn
喜欢
$subject_ids = DB::table('question_sets')
->select('subject_id')
->where('test_section_id','=',$testDetail->test_section_id)
->distinct()
->pluck('subject_id')
->toArray();
和
$topic_ids = DB::table('topics')
->select('id')
->whereIn('subject_id', $subject_ids)
->get();
推荐阅读
- windows - 如何测试启动powershell脚本的原因
- python - 如何在不同 .py 文件上的 Kivy 屏幕之间切换
- sqlite - Sqlite 按 AptNo 检索数据组并生成 7 天和 14 天的平均值
- docker - Docker 错误(等待标头时超出 Client.Timeout)
- python - 在 Dockerized 项目中使用带有 Poetry 的本地 python 项目
- excel - 如何使用模板回复所有人?
- javascript - 从字符串中删除特定的非字母数字字符
- amazon-web-services - 使用 terraform 创建实例时传递 ssh_authorized_key
- javascript - 如何生成一个最小化 printjs 或另一个 javascript 库的文件
- javascript - 尝试使用 Apollo 查询重新获取数据