php - How to use array push in laravel

72

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());
        }
    }
980

Answer

Solution:

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

Yourdata_chart array has a nested array nameddata 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.

578

Answer

Solution:

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

People are also looking for solutions to the problem: php - Why render function in ExceptionHandler in laravel dosen't execute?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.