PHPackages                             vildanbina/laravel-model-json - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. vildanbina/laravel-model-json

ActiveLibrary[Database &amp; ORM](/categories/database)

vildanbina/laravel-model-json
=============================

Laravel Model JSON

v2.5(5mo ago)12522.1k↓12.8%12MITPHPPHP ^8.1

Since Jan 29Pushed 5mo ago4 watchersCompare

[ Source](https://github.com/vildanbina/laravel-model-json)[ Packagist](https://packagist.org/packages/vildanbina/laravel-model-json)[ Docs](https://github.com/vildanbina/laravel-model-json)[ GitHub Sponsors](https://github.com/vildanbina)[ RSS](/packages/vildanbina-laravel-model-json/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (3)Versions (9)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/91397a1ba2dc7eb96ca4ebfc9689890a574d6cd49efb4a2b69737f623116ca6f/68747470733a2f2f706f7365722e707567782e6f72672f76696c64616e62696e612f6c61726176656c2d6d6f64656c2d6a736f6e2f76)](https://packagist.org/packages/vildanbina/laravel-model-json)[![Total Downloads](https://camo.githubusercontent.com/1ec5d59ae925bcbad559d2d7aa578352d37968cb300906dc5ee530c3d9a7f515/68747470733a2f2f706f7365722e707567782e6f72672f76696c64616e62696e612f6c61726176656c2d6d6f64656c2d6a736f6e2f646f776e6c6f616473)](https://packagist.org/packages/vildanbina/laravel-model-json)[![Latest Unstable Version](https://camo.githubusercontent.com/6a076556eeff467594f3a92cef09e1d14c1e497bd12ec51bf78d48c1fd98f9bb/68747470733a2f2f706f7365722e707567782e6f72672f76696c64616e62696e612f6c61726176656c2d6d6f64656c2d6a736f6e2f762f756e737461626c65)](https://packagist.org/packages/vildanbina/laravel-model-json)[![License](https://camo.githubusercontent.com/66e09280f064e6a6ded397699f730bf41a384b4de13828da4737479613bc8ef9/68747470733a2f2f706f7365722e707567782e6f72672f76696c64616e62696e612f6c61726176656c2d6d6f64656c2d6a736f6e2f6c6963656e7365)](https://packagist.org/packages/vildanbina/laravel-model-json)[![PHP Version Require](https://camo.githubusercontent.com/49a7a943fa08446e2b1c4670d07c1b356feb0ef5e9b86ef1e8b09e39ed196dca/68747470733a2f2f706f7365722e707567782e6f72672f76696c64616e62696e612f6c61726176656c2d6d6f64656c2d6a736f6e2f726571756972652f706870)](https://packagist.org/packages/vildanbina/laravel-model-json)

Introduction
------------

[](#introduction)

The Laravel Model JSON package allows you to export data from a specific model in the form of a JSON file. It is based on the `php artisan` command and is easy to use.

Installation
------------

[](#installation)

To install this package, use the following command:

```
composer require vildanbina/laravel-model-json
```

Usage
-----

[](#usage)

Export
======

[](#export)

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.

For example, to export data from the `User` model, you would run the following command:

```
php artisan model:export User
```

If your model is located in a different folder, you can specify the exact location, like so:

```
php artisan model:export App\Models\User
```

Options
-------

[](#options)

### Choose your path to save

[](#choose-your-path-to-save)

This package also has several options that allow you to customize the export functionality. For example, you can use the `--path=public` option to save the JSON data in a different folder. Here's an example:

```
php artisan model:export User --path=public
```

### Filename

[](#filename)

By default, the filename of the JSON data is "Model-Timestamp", but you can also specify a custom filename using the `--filename=data` option. For example:

```
php artisan model:export User --filename=data
```

### Except Fields from export

[](#except-fields-from-export)

You can also exclude certain columns from the export by using the `--except-fields` option. This is useful if you only want to export certain data from the model. For example:

```
php artisan model:export User --except-fields=id,deleted_at
```

### Without timestamps

[](#without-timestamps)

To exclude the `created_at`, `updated_at`, and `deleted_at` columns from the export, use the `--without-timestamps` option. For example:

```
php artisan model:export User --without-timestamps
```

### Without global scopes

[](#without-global-scopes)

You can remove registered global scopes from the export with the `--without-global-scopes` option. For example:

```
php artisan model:export User --without-global-scopes
```

### With hidden

[](#with-hidden)

By default, only visible fields are included in the export. To also include all hidden fields in the export, use the `--with-hidden` option. For example:

```
php artisan model:export User --with-hidden
```

This will also apply to any included relation(s) if used in combination with the `--with-relationships` option.

### Select only specific fields

[](#select-only-specific-fields)

If a model has a large number of columns and you only want to export a subset of them, you can use the `--only-fields` option. This allows you to specify which columns you want to include in the export. For example:

```
php artisan model:export User --only-fields=name,email
```

### Forget data

[](#forget-data)

You can forget data from the export by using the dot notation, accepting wildcards using asterisks. For example:

```
php artisan model:export Post --forget-data=comments.*.moderated_at
```

This can be useful if you include relations with the `--with-relationships` option and you would like to remove `chaperone()`'d relations from the nested data.

The `--forget-data` option supports one or more keys, comma separated.

### Apply a specific scope to the query

[](#apply-a-specific-scope-to-the-query)

If you wish to apply a scope to the model query because you wish to exclude certain records, you can use the `--scope={scope}` option. This allows you to specify a scope for the records you want to include in the export. For example:

```
php artisan model:export User --scope=verified
```

On your `User` model you would have the following method:

```
    public function scopeVerified(Builder $query): void
    {
        $query->whereNotNull('email_verified_at');
    }
```

The `--scope` option also supports one or more arguments to be passed to the scope, comma separated. For example:

```
php artisan model:export User --scope=byEmail,foo@example.com
```

On your `User` model you would have the following method:

```
    public function scopeByEmail(Builder $query, string $email): void
    {
        $query->where('email', $email);
    }
```

### Relationships

[](#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 `+` 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
```

If you want to export a Product model along with both its Category and Supplier relationships, you can use the command:

```
php artisan model:export Product --with-relationships=category+supplier
```

Additionally, you can choose to only export specific columns of the relationship by using the syntax `{relationship_name}:{columns_to_export}`.

For example, if you want to export a `Product` model along with its `Category` relationship and only export the `id` and `name` columns of the `Category`, you can use the command:

```
php artisan model:export Product --with-relationships=category:id,name
```

If you want to save JSON in a file as a beautified version, you can use the `--beautify` option or its shorthand `-b`. For example:

```
php artisan model:export User --beautify

#or

php artisan model:export User -b
```

By default, it will be exported to an inline JSON.

---

Import
======

[](#import)

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:

Parameters
----------

[](#parameters)

- `model`: Required. The name of the model to be imported.
- `path`: Required. The path to the JSON file, which must contain valid JSON data.

Example:

```
php artisan model:import User public/Users.json
```

This command will store all the data found in the JSON file in the database.

Except Fields from importing
----------------------------

[](#except-fields-from-importing)

You can exclude specific columns by using the `--except-fields` option, separated by commas, ex:

```
php artisan model:import User public/Users.json --except-fields=email_verified_at
```

You can also exclude timestamps by using the `--without-timestamps` option.

Select only specific fields to import
-------------------------------------

[](#select-only-specific-fields-to-import)

If you only want to store specific fields, you can use the `--only-fields` option, separated by commas. Ex:

```
php artisan model:import User public/Users.json --only-fields=first_name,last_name,email
```

Forget data
-----------

[](#forget-data-1)

You can forget data from the import by using the dot notation, accepting wildcards using asterisks. For example:

```
php artisan model:import Post public/Posts.json --forget-data=comments.*.moderated_at
```

The `--forget-data` option supports one or more keys, comma separated.

Update existing records
-----------------------

[](#update-existing-records)

You can update existing records in the database instead of creating duplicates by using the `--update-when-exists` option, ex:

```
php artisan model:import User public/Users.json --update-when-exists
```

If you want to group the updates based on a different column, you can use the `--update-keys option`. The records will be updated based on the matching existing records.

```
php artisan model:import User public/Users.json --update-when-exists --update-keys=email
```

Note: The `--update-when-exists` option must be present in order for the update feature to be enabled.

Relationships
-------------

[](#relationships-1)

In addition to importing models from JSON, this package also allows you to import relationships between models. Currently supported relationship types are:

- HasOne
- HasMany
- HasOneThrough
- HasManyThrough
- MorphOne
- MorphMany
- MorphToMany
- MorphTo
- BelongsTo
- BelongsToMany

You can import 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 `+`symbol if you want to attach more than one relationship.

For example, if you want to import a Category model along with its Product relationship, you can use the command:

```
php artisan model:import Category public/Categories.json --with-relationships=products
```

If you want to import a Category model along with both its Product and User relationships, you can use the command:

```
php artisan model:import Category public/Categories.json --with-relationships=products+user
```

Additionally, you can choose to only import specific columns of the relationship by using the syntax `{relationship_name}:{columns_to_import}`.

For example, if you want to import a `Category` model along with its `Product` relationship and only import the `id`and `name` columns of the `Product`, you can use the command:

```
php artisan model:import Category public/Categories.json --with-relationships=products:id,name
```

**Note:** In addition to the assignment that will be done in the above examples for importing a product to a category with relationships, Category will also be updated with the attributes found in the JSON.

---

Conclusion
----------

[](#conclusion)

The Laravel Model JSON package is a useful tool for exporting data from a specific model in a JSON format. It offers various options to customize the export process and make it more convenient for your needs. Give it a try and see how it can help you in your project.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please e-mail  to report any security vulnerabilities instead of the issue tracker.

Credits
-------

[](#credits)

- [Vildan Bina](https://github.com/vildanbina)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance73

Regular maintenance activity

Popularity44

Moderate usage in the ecosystem

Community18

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 69% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~150 days

Recently: every ~124 days

Total

8

Last Release

154d ago

Major Versions

v1.1 → v2.02023-04-22

PHP version history (2 changes)v1.0PHP ^8.0|^8.1

v2.3PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/5cd0156aac23b34c36c7e291fa0d655a6bd0ceecef941ba458e56f66854536e0?d=identicon)[vildanbina](/maintainers/vildanbina)

---

Top Contributors

[![vildanbina](https://avatars.githubusercontent.com/u/51203303?v=4)](https://github.com/vildanbina "vildanbina (20 commits)")[![RemiHin](https://avatars.githubusercontent.com/u/56312208?v=4)](https://github.com/RemiHin "RemiHin (4 commits)")[![onlime](https://avatars.githubusercontent.com/u/2759561?v=4)](https://github.com/onlime "onlime (3 commits)")[![jakobploens](https://avatars.githubusercontent.com/u/1315500?v=4)](https://github.com/jakobploens "jakobploens (1 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (1 commits)")

---

Tags

laravelmodel-json-exportmodel-json-importlaravelmodel-exportjson-export

### Embed Badge

![Health badge](/badges/vildanbina-laravel-model-json/health.svg)

```
[![Health](https://phpackages.com/badges/vildanbina-laravel-model-json/health.svg)](https://phpackages.com/packages/vildanbina-laravel-model-json)
```

###  Alternatives

[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spatie/laravel-model-flags

Add flags to Eloquent models

4301.1M1](/packages/spatie-laravel-model-flags)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[spatie/laravel-sql-commenter

Add comments to SQL queries made by Laravel

1931.4M1](/packages/spatie-laravel-sql-commenter)[spatie/laravel-deleted-models

Automatically copy deleted records to a separate table

409109.8k4](/packages/spatie-laravel-deleted-models)[wnx/laravel-backup-restore

A package to restore database backups made with spatie/laravel-backup.

203330.1k2](/packages/wnx-laravel-backup-restore)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
