Import Export Database in JSON Format Laravel

  • 06-02-2023
  • 527
  • Laravel 9
  • Haresh Chauhan

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();

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.

    $data2 = App\Models\Category::orderBy('id','desc')->get()->toArray();

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');

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.


    $data2 = json_decode(file_get_contents(public_path('Category.json'))) ;

    foreach ($data2 as $key => $value2) {

        $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

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


We Are Also Recommending You :