为了账号安全,请及时绑定邮箱和手机立即绑定

在laravel中的where子句中使用一个查询结果的方式是什么,就像我们在SQL查询中使用

在laravel中的where子句中使用一个查询结果的方式是什么,就像我们在SQL查询中使用

PHP
明月笑刀无情 2021-05-07 22:35:09
我在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();
查看完整描述

1 回答

?
千万里不及你

TA贡献1784条经验 获得超9个赞

在下面的查询


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


查看完整回答
反对 回复 2021-05-21
  • 1 回答
  • 0 关注
  • 167 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信