简体   繁体   English

将数据推送到Laravel PHP中的多维数组

[英]push data in to a multidimensional array in laravel php

I want to create a multidimensional array to save the data according the date and a category as follow. 我想创建一个多维数组以根据日期和类别保存数据,如下所示。 Then i need to display this data in my blade view?what can i do to achieve this. 然后,我需要在刀片视图中显示此数据?我该怎么做才能做到这一点。

'2012-05-05' => array(
    'suspension' => 52,
    'transmission' => '58'
),
'2012-05-05' => array(
    'suspension' => 44,
    'transmission' => 21

I have done the following in my controller i want a $reportData variable to load the data. 我已经在控制器中完成了以下操作,我希望使用$ reportData变量加载数据。

 public function loadReports(Request $request)
{
    $data = ['2012-05-05','2012-05-06'];
     $salesItems = array();



        $orderItems = OrderItem::with('spare', 'order')->get();

        foreach ($orderItems as $key => $orderItem) {


            if ($orderItem->spare->retailer_id == Auth::user()->id) {
                array_push($salesItems, $orderItem);
            }


        }
        $categories = App\Categories::all();

        foreach ($data as $date) {

            foreach ($categories as $category) {
                $categoryValue = 0;
                foreach ($salesItems as $salesItem) {
                    if ($date == $salesItem->order->orderDate) {
                        $categoryValue += $categoryValue + $salesItem->subTotal;
                    }
                }
                //error appears as illegal offset type
                $reportData[$date][$category]=$categoryValue;



            }
        }


    return View::make('Retailer/reports')->with('categories', $categories)->with('reportData', $reportData);
}

I haven't tested it but looking at your code it seems that you're passing an object as array index key as 2nd level array index: 我还没有测试过它,但是查看您的代码,看来您正在将对象作为数组索引键作为第二级数组索引传递:

$reportData[$date][$category] = $categoryValue;
                   ^^^^^^^^^ this is an object

Dump your $category in the foreach loop & check if that is the case: dd($category) $category转储到foreach循环中,然后检查是否是这种情况: dd($category)

If you're using Eloquent & your Categories Model has a name property, you'll probably want to take each category name as index value: 如果您使用的是Eloquent并且您的类别模型具有name属性,则可能需要将每个类别名称用作索引值:

$reportData[$date][$category->name] = $categoryValue;

The error is occurring due to the fact that you are trying to use an Object as the array's index. 由于您尝试使用对象作为数组的索引,因此发生了错误。

As per the laravel documentation ( https://laravel.com/api/4.2/Illuminate/Database/Eloquent/Model.html#method_all ) the all method you called here '$categories = App\\Category::all();' 根据laravel文档( https://laravel.com/api/4.2/Illuminate/Database/Eloquent/Model.html#method_all ),您在此处调用的all方法为'$ categories = App \\ Category :: all();' would have returned an Eloquent Collection. 会返回一个雄辩的收藏。

So when you iterated over the $categories array and referenced $category, you were referencing an object. 因此,当您遍历$ categories数组并引用$ category时,您正在引用一个对象。 In PHP an array can only be indexed by either an integer or a string. 在PHP中,数组只能用整数或字符串索引。 So you need to change the line of code where the error is to this 因此,您需要更改错误所在的代码行

$reportData[$date][$category->someVar] = $categoryValue;

Where someVar is the name of a variable on the Eloquent model Category that references its name, such as 'suspension' etc. 其中someVar是Eloquent模型类别上引用其名称的变量名称,例如“ suspension”等。

While it doesn't answer your question you could use the Eloquent engine to make your life easier: 虽然它不能回答您的问题,但是您可以使用Eloquent引擎使您的生活更轻松:

$orderItems = OrderItem::with('spare', 'order')->get();

foreach ($orderItems as $key => $orderItem) {
    if ($orderItem->spare->retailer_id == Auth::user()->id) {
        array_push($salesItems, $orderItem);
    }
}

can be simplified (and made more efficient) with: 可以简化(并提高效率):

// Store the uid to save the call.
$user_id = Auth::user()->id;

// Apply the condition to the Eloquent query.
$orderItems = OrderItem::with(['spare' => function($query) use ($user_id) {
        return $query->where('retailer_id', '=', $user_id);
}, 'order'])->get();

The other answers are correct, but you probably also want to initialise the $reportData array as before you start working with it. 其他答案是正确的,但是您可能还希望像开始使用它之前一样初始化$reportData数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM