dsbj66959 2017-02-22 15:24
浏览 6
已采纳

too long

I have a query which fetches one table and then the results of that query gets passed though another query.

Then I want to return all the values of that column in each row from that query.

This fetches all of the Product id's with a particular brand ID

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

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

Then print_r($fetchID) returns the array.

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 )

I then have a table with the fields product_id and category_id, so I want to pass the results of $fetchID though and return all values from the category_id using lists()

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

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

This returns as empty in print_r

Finally I want to query the categories table which has id and name, and return everything. So I try to pass the $catRelList through. This doesn't work because in the previous query it returns as empty.

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

So my issue is passing though $fetchID to return all rows matching the multiple Product ID's. As when I manually enter a product ID it returns that category fine. Query Below

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

Now where the 10011 is, I somehow want to pass multiple values through, like in the $fetchID array.

Is this possible to do and is there a better method of doing this?

  • 写回答

1条回答 默认 最新

  • donglan9651 2017-02-22 15:34
    关注

    Change your $catRelation query to using whereIn:

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

    This should now correctly using your array of ProductIDs and find all matching rows. (Change column name as required).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)