peachananr/onepage-scroll

PHP - Laravel JSON create

Razvan-BT opened this issue · 1 comments

I want to create a JSON file, in API using Laravel 10, vue and Axios, how can i do this? Thanks

I want the best way

I want to create a JSON file, in API using Laravel 10, vue and Axios, how can i do this? Thanks

I want the best way

I think you are asking for how to make a trigger from vue frontend to crate a JSON file in laravel backend using axios....

Thinking that is the question:

  1. Create a controller -> php artisan make:controller AnyNameContoller
  2. Create the function inside "AnyNameController" to crate the JSON file. -> `public function createJsonFile(Request $request) {
    $data = ['key' => 'value'];
    file_put_contents(public_path('example.json'), json_encode($data));

return response()->json(['message' => 'JSON file created successfully']);
}`

  1. Create the necessary rotes in routes/api.php -> Route::post('/create-json-file', 'ApiController@createJsonFile');
  2. If you dont have axios install axios using npm install axios
  3. Create the necessary component or the triggering element in vue at set axios like this
    axios.post('/api/create-json-file') .then(response => { console.log(response.data.message); }) .catch(error => { console.error(error); });
  4. Then run :)