2017-02-22 48 views
1

我有一個查詢獲取一個表,然後該查詢的結果通過另一個查詢傳遞。使用Eloquent提取具有多個ID的多行

然後,我想返回該查詢中每一行中該列的所有值。

這獲取所有產品的ID與特定的品牌標識

 // Fetch the Product List 
     $brandID = 1; 
     $prodList = Products::whereBrandId($brandID); 

     // Fetch the catalog ID of the Product 
     $fetchID = $prodList->lists('id'); 

隨後的print_r($ fetchID)返回數組。

Array ([0] => 10011 [1] => 10012 [2] => 10013 [3] => 10014 [4] => 10015 [5] => 10016 [6] => 10017 [7] => 10018 [8] => 10019 [9] => 10020 [10] => 10021 [11] => 10022 [12] => 10023 [13] => 10024 [14] => 10025 [15] => 10026 [16] => 10027 [17] => 10028 [18] => 10029 [19] => 10030 [20] => 10031 [21] => 10032 [22] => 10033 [23] => 10034 [24] => 10035 [25] => 10036 [26] => 10037 [27] => 10038 [28] => 10039 [29] => 10040 [30] => 10041 [31] => 10042 [32] => 10043 [33] => 10044 [34] => 10045 [35] => 10046 [36] => 10047 [37] => 10048 [38] => 10049 [39] => 10050 [40] => 10051 [41] => 10052 [42] => 10053 [43] => 10054 [44] => 10055 [45] => 10056 [46] => 10057 [47] => 10058 [48] => 10059 [49] => 10060 [50] => 10061 [51] => 10062 [52] => 10063 [53] => 10064 [54] => 10065 [55] => 10066 [56] => 10067 [57] => 10068 [58] => 10069 [59] => 10070 [60] => 10071 [61] => 10072 [62] => 10073 [63] => 10074 [64] => 10075 [65] => 10076 [66] => 10077 [67] => 10078 [68] => 10079 [69] => 10080 [70] => 10092 [71] => 10093 [72] => 10128) 

然後我與字段的表PRODUCT_ID和CATEGORY_ID,所以我想,雖然通過$ fetchID的結果,並使用列表()

// Fetch the category_id where is a product_id 
    $catRelation = Db::table('purple_catalog_prods_cats')->whereProductId($fetchID); 

    $catRelList = $catRelation->lists('category_id'); 

這將返回從CATEGORY_ID返回所有值as empty in print_r

最後,我想查詢具有id和name的類別表,並返回所有內容。所以我嘗試通過$ catRelList。這不起作用,因爲在之前的查詢中它返回爲空。

// Fetch the Cat list 
    $catList = categoryName::whereId($catRelList)->orderBy('id', 'asc'); 
    $this->categoryName = $catList->get(); 

所以我的問題是通過$ fetchID返回所有行匹配多個產品ID的。當我手動輸入產品ID時,它會返回該類別的罰款。下面的查詢

$catRelation = Db::table('purple_catalog_prods_cats')->whereProductId('10011'); 

現在,10011是,我不知何故想通過多個值,如在$ fetchID數組。

這是可能的,是否有更好的方法呢?

回答

2

您$ catRelation查詢更改爲使用whereIn

$catRelation = Db::table('purple_catalog_prods_cats')->whereIn('product_id', $fetchID); 

這個現在應該使用ProductIDs的陣列正確,找到所有匹配的行。 (根據需要更改列名稱)。

+0

非常感謝您的答案! – ServerSideSkittles