Follow this code.
function store(Request $request) { try { // my data storage location is project_root/storage/app/data.json file. $contactInfo = Storage::disk('local')->exists('data.json') ? json_decode(Storage::disk('local')->get('data.json')) : []; $inputData = $request->only(['name','age']); $inputData['datetime_submitted'] = date('Y-m-d H:i:s'); array_push($contactInfo,$inputData); Storage::disk('local')->put('data.json', json_encode($contactInfo)); return $inputData; } catch(Exception $e) { return ['error' => true, 'message' => $e->getMessage()]; } }
Question and answer is powered by AnsPress.io