3

Is it possible to get data from an api url and save it directly to database when working with laravel? the data i get from the url is of the format {"name":"100KVA SUKAM Generator","level":"5.965"}.

1 Answer 1

2

Yes, you can create table with json type field (or text) and keep data there:

$table->json('data_from_api');

https://laravel.com/docs/5.2/migrations#writing-migrations

If you want to persist data as usual data, you can use mass assignment. First, convert JSON to an array with [json_decode][1] and save data like that:

$data = json_decode($jsonData, true)
Model::create($data);

Don't forget to add all columns to a $fillable property of a model.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, i am a little bit confused on how to go about taking the information from the page. i know i have to use onload function to call the information to the view from the api, also do i use a post method and if correct how do i accomplish this. i really appreciate your help @alexey-mezenin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.