简体   繁体   中英

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.

 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)

If you're using Eloquent & your Categories Model has a name property, you'll probably want to take each category name as index value:

$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();' would have returned an Eloquent Collection.

So when you iterated over the $categories array and referenced $category, you were referencing an object. In PHP an array can only be indexed by either an integer or a string. 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.

While it doesn't answer your question you could use the Eloquent engine to make your life easier:

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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