簡體   English   中英

在laravel中的where子句中使用一個查詢結果的方式是什么,就像我們在SQL查詢中使用where in子句那樣?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM