Megandi Megandi - 2 years ago 204
JSON Question

Input JSON type data to Laravel 5.2

I have file txt (data.txt) with this value :

{"id":"1","title":"first Testing","completed":"Yes"}

{"id":"2","title":"Second Testing","completed":"no"}

{"id":"3","title":"Third Testing","completed":"no"}

and table Data with field
, and

how I can input all value in data.txt into my Table (Data) with laravel 5.2??

Thanks before.. and sorry for my bad english..

Answer Source

Read file and convert it to PHP Array

 $arrays = json_decode(file_get_contents('data.txt'), true);

Then with simple foreach

foreach($arrays as $array) {
   $myObject = new Model();
   $myObject->id = $array['id'];
   $myObject->title = $array['title'];
   $myObject->completed = $array['completed'];

Or you could do it straightfoward if you defined $fillable attributes

foreach($arrays as $array)

Eloquent: Mass Assignment

Using Query Builder


Query Builder: Insert

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download