Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Do you want to export and import the Database table in Laravel?
This step-by-step tutorial helps you learn the most common thing of how to export data from a specific model in the form of a JSON file in the Laravel application.
The process of Exporting and importing the model as JSON files in Laravel is a common thing, whenever we want to deploy our application locally to the live server, we also need to export and import database file to run the application.
Someone prefers JSON and someone prefers SQL to import and export.
The highlighted part of the package is, it could do imports with relationships using the Laravel Model JSON package.
Laravel Model JSON is a package that allows you to easily export data from specific models in your Laravel application to JSON format. It is based on the php artisan command and offers various options to customize the export process.
This package is helpful for exporting important data from your models in a convenient format.
Okay, let’s get straight to the steps for the Export and import model as a JSON file in Laravel with an Example 👇
Export Model as JSON
The command to export data from a model is php artisan model:export {model}, where {model} is the class name of the model you wish to export. After running this command, the data will be automatically saved in the storage/app folder.
php artisan model:export App\Models\User
Import Model as JSON
The model: import command allows you to import data from a JSON file and store it in your database.
For example, to import data for the User model, you would run the following command:
php artisan model:import User storage/app/User-2023-03-13-07-05-38.json
Export Model With Relationships
You can now export models along with their specified relationships using the new option –with-relationships={relations}. {relations} are the names of the relationships and can be separated by a + symbol if you want to attach more than one relationship.
For example, if you want to export a Product model along with its Category relationship, you can use the command:
php artisan model:export Product --with-relationships=category
Related article: How To Create Factory In Laravel 9.
I hope that this article helped you learn how to Import and Export data in Laravel 9. You may also want to check out our guide on How to Display Serial Number Examples in the Laravel application.