[英]What is the way to use the one query result in where clause in laravel like we do in sql query using where in clause?
我在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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.