猿问

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

我在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();


明月笑刀无情
浏览 169回答 1
1回答

千万里不及你

在下面的查询$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();
随时随地看视频慕课网APP
我要回答