In this post, I will tell you how to export and import MySQL or pgsql database in JSON file format, sometimes my SQL export file corrupts because of some reason so my application database is not safe when I take .sql database export backup from the database, after the long thing I decide to take database table take backup in JSON file format, JSON file format never corrupt so this is the best way to take your database backup.
Below example, I import and export the database for each table data in a separate JSON file format, also describe how to take a backup and how to insert that table data in the database using a loop I have given all the things in the below example.
This is a very simple and easy trick for importing and exporting your database in the JSON format file with the secure collection and making a database so let's see how to do that.
Example 1
In this example I am calling the "Advertisement" model and taking whole table data as an array and that data inserting in the "Advertisement.json" file using the "file_put_contents()" function.
$data1 = App\Models\Advertisement::orderBy('id','desc')->get()->toArray(); file_put_contents(public_path('Advertisement.json'),json_encode($data1,JSON_PRETTY_PRINT));
Example 2
In this example I am using Route for the calling action same as the above example given the calling model and inserting that array data in the JSON file, storing array data by encoding JSON format and inserting it in the file.
Route::get('export',function(){ $data2 = App\Models\Category::orderBy('id','desc')->get()->toArray(); file_put_contents(public_path('Category.json'),json_encode($data2,JSON_PRETTY_PRINT)); });
Example 3
If you want to import that export data, see the below example, in this example I am calling that JSON file through the php function "file_get_contents()" and inserting data using looping.
$data1 = json_decode(file_get_contents(public_path('Advertisement.json'))) ; foreach ($data1 as $key => $value1) { $value1->created_at = \Carbon\Carbon::parse($value1->created_at)->format('Y-m-d H:i:s'); $value1->updated_at = \Carbon\Carbon::parse($value1->updated_at)->format('Y-m-d H:i:s'); \DB::table('advertisements')->insert((array)$value1); }
Example 4
Call the route and insert data in the database table, I am also updating the date and time in the database which is why I am calling carbon date format.
Route::get('import',function(){ $data2 = json_decode(file_get_contents(public_path('Category.json'))) ; foreach ($data2 as $key => $value2) { // CONVERT DATE FORMAT $value2->created_at = \Carbon\Carbon::parse($value2->created_at)->format('Y-m-d H:i:s'); $value2->updated_at = \Carbon\Carbon::parse($value2->updated_at)->format('Y-m-d H:i:s'); // INSERT DATA \DB::table('categories')->insert((array)$value2); } });
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- How To Integrate Stripe Payment Gateway in Laravel 9
- How to Integrate PayUMoney Payment Gateway in Laravel 9
- Laravel Event Broadcast With Redis Socket.io Echo To Listen Real-Time Message
- How To Generate Digital Invoice PDF In Laravel Demo
- chunk() Method | Laravel Collection Example
- Pinelabs Payment Gateway Integration Tutorial In Laravel
- Laravel Upgrading To 10.0 From 9.x Version
- How To Generate A Sitemap In Laravel 6/7/8/9
- Arr::accessible() | Laravel Helper Function
- average() Method | Laravel Collection Example