我有一个获取一个表的查询,然后该查询的结果通过另一个查询传递。
然后我想返回该查询中每一行中该列的所有值。
这将获取具有特定品牌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的结果,并使用lists()返回category_id中的所有值
    // Fetch the category_id where is a product_id
    $catRelation = Db::table('purple_catalog_prods_cats')->whereProductId($fetchID);

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

打印时返回为空
最后,我想查询categories表,它有id和name,并返回所有内容。所以我试着把$catRelList传过去。这不起作用,因为在上一个查询中它返回为空。
    // Fetch the Cat list
    $catList = categoryName::whereId($catRelList)->orderBy('id', 'asc');
    $this->categoryName = $catList->get();

因此,我的问题是通过$fetchID来返回与多个产品ID匹配的所有行。当我手动输入一个产品ID时,它会返回该类别fine。以下查询
 $catRelation = Db::table('purple_catalog_prods_cats')->whereProductId('10011');

现在10011在哪里,我想传递多个值,比如在$fetchID数组中。
这样做可能吗?有更好的方法吗?

最佳答案

将$catRelation查询更改为使用whereIn

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

现在应该正确使用productid数组并找到所有匹配的行。(根据需要更改列名)。

关于php - 使用 Eloquent 来获取具有多个ID的多行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42395607/

10-09 20:12