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 2 months 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

Pardeeptech replied 2 months ago

I suggest you to update following code in config/filesystems.php, located in the config folder. In this file, you can specify the locations for your file storage.

return [
 
'default' => 'local',
 
'disks' => [
 
'local' => [
 
'driver' => 'local',
 
'root' => storage_path('app'),
 
],

more information related to storage visit this article :

https://www.cloudways.com/blog/laravel-multiple-files-images-upload/

Sign in to participate in this thread!



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