首页 > 解决方案 > passing json array form in the put storage method in laravel php

问题描述

iam trying to pass $data array to save it in the laravel storage files my code is :

$data = '[
            {
                "label": " Total Accidents",
                "color": "#5ab1ef",
                "data": 
                    [
                        ["Jan",0],
                        ["Feb",0],
                        ["Mar",0],
                        ["Apr",0],
                        ["May",0],
                        ["Jun",0],
                        ["Jul",0],
                        ["Aug",0],
                        ["Sep",0]
                    ]
            },
            {
                "label": " Active Experts",
                "color": "#f5994e",
                "data": 
                    [
                        ["Jan",0],
                        ["Feb",0],
                        ["Mar",0],
                        ["Apr",0],
                        ["May",0],
                        ["Jun",0],
                        ["Jul",0],
                        ["Aug",0],
                        ["Sep",0]
                    ]
            },
            {
                "label": " Pending Payments",
                "color": "#d87a80",
                "data": 
                    [
                        ["Jan",0],
                        ["Feb",0],
                        ["Mar",0],
                        ["Apr",0],
                        ["May",0],
                        ["Jun",0],
                        ["Jul",0],
                        ["Aug",0],
                        ["Sep",0]
                    ]
            }

      ]';   
 $test = json_decode($data, true);
 $test[0]['data'][1] =  ["Jan", 153];
 Storage::disk('chartData')
    ->put('chart/test'.$_SESSION['companyId'].'.json', $test);
 return 'done';

i want to change the values inside each year according to database values , the problem is that when iam trying to put method iam getting the following error :

Array to string conversion

if i pass $data array without doing json_decode it works fine , but in this case i cant change the data isnide the array , so i have to decode it .

标签: phpjsonlaravelstorage

解决方案


在这种情况下,您应该使用json_encode函数将 JSON 数据转换为字符串。

$test[0]['data'][1] =  ["Jan", 153];

$test = json_encode($test);// convert to string

Storage::disk('chartData')
->put('chart/test'.$_SESSION['companyId'].'.json', $test);

推荐阅读