Zaqi Salman Alexa
Zaqi Salman Alexa

Reputation: 70

How to use array push in laravel

I am trying to use array push to add new item in Laravel array but I got some trouble. I want add new item named 'Color' in 'data_chart'. I've been tried use array Push and array Unshift. Is there any better way to do this?

I want result like this :

"data": [
     {
       "id": 29,
       "title": "get data users",
       "function_name": "selectDataUser",
       "function_drop": "selectDataUser",
       "type_of_chart": "Pie",
       "embed": null,
       "created_at": "2019-06-15 03:26:09.000",
       "updated_at": null,
       "data_chart": {
           "data": [
            {
               "name": "Administrator",
               "total": "100",
               "color" "#9c1d1d" //I want color should be here in this line
             },
             {
                "name": "Staff",
                "total": "100",
                "color" "#9c1d1d" //new item named Color
              },
            ],
       }
     }
    ]

But when I try to use array push the result become like this :

"data": [
         {
           "id": 29,
           "title": "get data users",
           "function_name": "selectDataUser",
           "function_drop": "selectDataUser",
           "type_of_chart": "Pie",
           "embed": null,
           "created_at": "2019-06-15 03:26:09.000",
           "updated_at": null,
           "data_chart": {
              "data": [
                {
                   "name": "Administrator",
                   "total": "100"
                    //color should be in here
                 },
                 {
                    "name": "Staff",
                    "total": "100"
                    //color should be in here
                  }
                ],
              "color": "#9c1d1d" //idk but the color shouldn't like this
           }
         }
        ]

and this is my code :

 public function index(Request $request)
    {
        try {
            $query = $this->dashboard->with('rolesDashboard','userDashboard')
                                    ->orderBy('id')
                                    ->get();

            $data=[];
            foreach ($query as $key) {

            if (empty($key->store_procedure)) {
                $key->function_name = "";
                $data_chart = "";
            }else{
                try {
                    //data chart
                    $data_chart = DB::select($key->function_name); //call store procedure from SQL Server
                } catch (SqlException  $sqlEx) {
                    return response()->default(400, $sqlEx->getMessage());
                }
            }

            $color = "#9c1d1d"; //color value
            array_push($data_chart, $color); //here's the code

            $data[] = [
                'id' => $key->id,
                'title' => $key->title,
                'function_name' => $key->function_name,
                'function_drop' => $key->function_drop,
                'type_of_chart' => $key->type_of_chart,
                'embed' => $key->embed,
                'created_at' => $key->created_at,
                'updated_at' => $key->updated_at, 
                'data_chart' => $data_chart, //call data_chart
            ];  
            }
            return response()->default(200, trans('messages.success.get-data'),$data);
        } catch (Exception $e) {
            return response()->default(400, $e->getMessage());
        }
    }

Upvotes: 1

Views: 1624

Answers (2)

pr1nc3
pr1nc3

Reputation: 8358

You are using array push outside of the array you want to push actually.

Your data_chart array has a nested array named data so what you could do is loop around the array like:

foreach($data['data_chart'] as $row){
    $row->color = "#9c1d1d";  // you are inside the nested data array
}

You can use the above loop when you have constructed your $data array and want to add the extra item (color) in it.

Upvotes: 1

Lakhwinder Singh
Lakhwinder Singh

Reputation: 5582

Laravel provides us array helper functions, by which we can do this stuff without loop.

data_set($data_chart, 'data.*.color', $color);

For more array related helper function click here

Upvotes: 0

Related Questions