Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP - Laravel JSON create #366

Open
Razvan-BT opened this issue Oct 31, 2023 · 1 comment
Open

PHP - Laravel JSON create #366

Razvan-BT opened this issue Oct 31, 2023 · 1 comment

Comments

@Razvan-BT
Copy link

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
Copy link

Poori99 commented Nov 10, 2023

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants