I try to import JSON formatted file into MySQL database. I put this code in the web.php:
Route::get('/pages', function(){ $json = file_get_contents(storage_path('posts_and_comments.json')); $objs = json_decode($json,true); foreach ($objs as $obj) { foreach ($obj as $key => $value) { $insertArr[Str::slug($key,'_')] = $value; } DB::table('my_likes')->insert($insertArr); } dd("Finished adding data in examples table"); });
It gave me a ErrorException Array to string conversion Laravel. How can I solve it? Thanks!
Answer
Can you try encoding array as a json object than insert it to db ?
foreach ($objs as $obj) { foreach ($obj as $key => $value) { $insertArr[Str::slug($key,'_')] = json_encode($value); } DB::table('my_likes')->insert($insertArr); }