PHP - Laravel JSON create
Razvan-BT opened this issue · 1 comments
Razvan-BT commented
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
Poori99 commented
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:
- Create a controller -> php artisan make:controller AnyNameContoller
- 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']);
}`
- Create the necessary rotes in
routes/api.php
->Route::post('/create-json-file', 'ApiController@createJsonFile');
- If you dont have axios install axios using
npm install axios
- 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); });
- Then run :)