Back

store form input data to json file


I want to store form data in a json file located in storage/app/data.json with this controller

class ContactController extends Controller
{
    function index() {
        return view('contact_form');
      }
   
      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', 'email', 'message','subject']);
           
            $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()];
 
        }
      }
}

and this is the route for the form page

Route::get('/', '[email protected]');
Route::post('/contact/store', '[email protected]');

but it return's with this ERROR

(FatalThrowableError  class'App\http\controllers\storage' not found)

how to correct the path so the APP will search for the json file in the storage folder instead of the app folder to skip this ERROR?

Alexandru replied 1 week ago

seems like you have a class not imported for use "use Storage;" should fix the problem or just add a "/" becore your Storage::class


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us