管理员浏览订单时显示类别和子类别

时间:2016-10-18 09:09:15

标签: php laravel-4

当客户提交订单时,它将存储在数组中的数据库表orders中。该数组看起来像:

{
    "15": // product_id
    {
        "title":"Test of title", // product_title
        "description":"Test of description", // product_description
        "quantity":1,
        "price":132 
    },
    "shipping": // selected shipping information
    {
        "title":"Normal Delivery" - $10.00",
        "price":10
    }
}

然后在我的订单中Model我有了这个

public function getOrderData($data) { 
    return json_decode($data); 
}

在控制器中

public function orders() {

    $orders = Order::select('*')->paginate(10);
        return View::make('admin.orders', [
            'orders' => $orders
        ]);
}

在视图中,我已经制作了一个foreach并显示了此数组中的订单信息,如

@foreach($order->getOrderData($order->data) as $itemId => $item)     
    Title of product
    Description of product
    Price of product        
@endforeach

我想要制作的是相同的视图,但在Title of product前面还要显示类别名称和子类别名称,例如

@foreach($order->getOrderData($order->data) as $itemId => $item) 

    Category 1 -> Sub_category 1 -> Title of product
    .... // other info
@endforeach

问题:我在该数组和订单表中根本没有categorysub-category的任何内容。尝试连接表类别和子类别,但它们order表中的列没有product表我存储category_idsub_category_id

我该如何展示它们?

更新2:在此订单模型中更改了getOrderData

public function getOrderData($data) {

    $dataArray = json_decode($data, true);
    $data = json_decode($data);
    $arrayKeys = array_keys($dataArray);
    $arrayKeys = array_filter($arrayKeys, function($value) {
        return ($value !== 'shipping');
    });
    $productIds = implode(',', $arrayKeys);

    $products = 
        DB::table('products')
        ->join('category', 'products.product_id', '=', 'category.category_id')
        ->join('sub_category', 'products.sub_cat_id', '=', 'sub_category.sub_cat_id')
        ->whereIn('product_id',$productIds) ;



    foreach ($products as $item) {
        if(!in_array($item['product_id'], $arrayKeys)) continue;
        $dataArray[$item['product_id']]['category'] = $item['category']; // depending on the implementation $item may be array or an object! so this code may vary
        $dataArray[$item['product_id']]['subcategory'] = $item['subcategory'];
    }
    return (object) $dataArray;   
  //return json_decode(json_encode($dataArray));        
} 

收到错误:

  

array_keys()期望参数1为数组,给定对象

更新:phpmyadmin中的此查询返回了id = 9的所有产品信息+类别和子类别的所有信息

SELECT * 
FROM products AS p
LEFT JOIN category AS c ON p.category_id =1
LEFT JOIN sub_category AS sc ON p.sub_cat_id =1
WHERE p.product_id =9

2 个答案:

答案 0 :(得分:1)

我可以给你这个解决方案: 从表中获取订单的结果后,您可以迭代订单以获取productID。然后选择这样的查询:

SELECT fields FROM products AS p
JOIN categories AS c ON ...
JOIN sub_categories AS sc ON ...
WHERE p.id IN (productIDs)

获取产品ID的代码是这样的:

public function getOrderData($data) {
    $dataArray = json_decode($data, true);
    $data = json_decode($data);
    $arrayKeys = array_keys($dataArray);
    $arrayKeys = array_filter($arrayKeys, function($value) {
        return ($value !== 'shipping');
    });
    $productIds = implode(',', $arrayKeys);

    // QUERY FOR selecting product categories, so when this query return results you can merge them with order data.
    $productsInfo = ....

    foreach ($productsInfo as $item) {
        if(!in_array($item['productId'], $arrayKeys)) continue;
        $dataArray[$item['product_id']]['category'] = $item['category_name']; // depending on the implementation $item may be array or an object! so this code may vary
        $dataArray[$item['product_id']]['subcategory'] = $item['sub_cat_name '];
    }

    // If products is array of objects use this code
    //foreach ($productsInfo as $item) {
    //    if(!in_array($item->productId, $arrayKeys)) continue;
    //    $dataArray[$item->product_id]['category'] = $item->category_name; // depending on the implementation $item may be array or an object! so this code may vary
    //    $dataArray[$item->product_id]['subcategory'] = $item->sub_cat_name ;
    //}
    return (object) $dataArray;
}

如果return (object) $dataArray;没有给出与您问题中的回复相同的回复结构,那么:

return json_decode(json_encode($dataArray)); // for example

如果您添加有关表格结构的其他信息,我可以给您更好的答案。

答案 1 :(得分:1)

试试这个;)

执行json_decode时,它会将关联数组转换为对象,这就是传递此解码后的json数据时出现此错误的原因;因此,在将此解码数据cast用于array;

之前
$data = (array) json_decode($data);