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

Php 管理员浏览订单时显示类别和子类别,php,laravel-4,Php,Laravel 4,当客户提交订单时,它以数组形式存储在数据库表orders中。该数组看起来像: { "15": // product_id { "title":"Test of title", // product_title "description":"Test of description", // product_description "quantity":1, "price":132 }, "shippi

当客户提交订单时,它以数组形式存储在数据库表
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
    }
}
然后在我的订单中,我有这个

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
我想做的是相同的视图,但在产品的
标题前面
也显示类别名称和子类别名称,如

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

    Category 1 -> Sub_category 1 -> Title of product
    .... // other info
@endforeach
问题:我在该数组和orders表中根本没有关于
类别
子类别
的任何内容。尝试联接表类别和子类别,但它们在
顺序中没有列
仅在
产品
表I存储
类别id
子类别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

我可以为您提供以下解决方案: 从表中获得订单的结果后,可以迭代订单以获得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;
}
如果
返回(object)$dataArray没有给出与问题中的回答相同的回答结构,那么:

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

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

我可以为您提供以下解决方案: 从表中获得订单的结果后,可以迭代订单以获得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;
}
如果
返回(object)$dataArray没有给出与问题中的回答相同的回答结构,那么:

return json_decode(json_encode($dataArray)); // for example
如果您添加有关表结构的其他信息,我可以给您提供更好的答案。

试试这个;)

当您执行
json\u decode
时,它会将关联数组转换为对象,这就是为什么在传递此解码的json数据时会出现此错误;因此,在使用此解码数据之前,
将其转换为
数组

$data = (array) json_decode($data);
试试这个;)

当您执行
json\u decode
时,它会将关联数组转换为对象,这就是为什么在传递此解码的json数据时会出现此错误;因此,在使用此解码数据之前,
将其转换为
数组

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

谢谢你的回答。查询很好,我可以更改字段的位置。我不知道如何生成
在从表中获得订单的结果后,您可以迭代订单以获得ProductID
您可以给我一个示例吗?我编辑了我的答案并添加了您要求的代码。谢谢。我已经更新了我的问题,我已经尝试了什么,我有什么错误。所以我试着实现你的解决方案,但到目前为止出现了这个错误。刚刚编辑了答案。。。有两个json_解码有点难看,但我不知道你的实现$data to be数组是否会有问题,所以这就是为什么我又添加了一个$DATARRAY:)我很高兴我们终于找到了问题的解决方案。谢谢你的回答。查询很好,我可以更改字段的位置。我不知道如何生成
在从表中获得订单的结果后,您可以迭代订单以获得ProductID
您可以给我一个示例吗?我编辑了我的答案并添加了您要求的代码。谢谢。我已经更新了我的问题,我已经尝试了什么,我有什么错误。所以我试着实现你的解决方案,但到目前为止出现了这个错误。刚刚编辑了答案。。。有两个json_解码有点难看,但我不知道您的实现$data to be数组是否会有问题,所以这就是为什么我又添加了一个$DATARRAY:)我很高兴我们终于找到了问题的解决方案。谢谢。他也这样工作。现在的问题是如何从query+$data返回两个结果以供查看..谢谢。他也这样工作。现在的问题是如何从query+$data返回两个结果以供查看。。