2017 © Pedro Peláez
 

library eloquent-export

A CLI tool for exporting and importing database data using Laravel's Eloquent models

image

faulker/eloquent-export

A CLI tool for exporting and importing database data using Laravel's Eloquent models

  • Sunday, April 29, 2018
  • by faulker
  • Repository
  • 1 Watchers
  • 2 Stars
  • 6,404 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 5 Versions
  • 15 % Grown

The README.md

Laravel Eloquent Export Tool

Give the ability to build profiles/configurations to be used by a artisan command to export data from the database using Eloquent models., (*1)


This has only been tested on Laravel 5.1 but it should work on higher versions, (*2)


Use case:

  • Export a user and all there data from a production database and import it into a local database for bug testing/repairing.
  • Backup subset of data so that you can easily restore it after testing new features or changes to the data stored in the database.

Composer Install

Add the fallowing to your composer.json, (*3)

{
    "require": {
        "faulker/eloquent-export": "dev-master"
    }
}

Add the service provider to config/app.php, (*4)

Faulker\EloquentExport\EloquentExportServiceProvider::class,

Publish the default config file config/eloquent-export.php, (*5)

php artisan vender:publish

5.1 Notes

Laravel 5.1 dosen't support the ability of getting a list of a Model's casted columns so you will have to add the Faulker\EloquentExport\EloquentExportTrait trait to any Model that has JSON or array casted columns., (*6)

use Faulker\EloquentExport\EloquentExportTrait;

class MyModel extends Models
{
    use EloquentExportTrait;
    ...
}

Profile Creation

Profile Structure

'profile_name' => [
    'model'     => \Name\Space\Root\Model::class,
    'relations' => [
        '[relation]'                  => \Name\Space\Relation\Model::class,
        '[relation].[child_relation]' => \Name\Space\ChildRelation\Model::class,
    ],
],

Example, (*7)

'user_posts' => [
    'model'     => \Name\Space\EloquentUser::class, // User model (root model)
    'relations' => [
        'posts'         => \Name\Space\Posts::class, // Posts model
        'posts.comment' => \Name\Space\Comments::class, // Comments model
    ],
],

The above profile will export a user, all their posts, and all comments for each post., (*8)

Usage

Base arguments

php artisan export:eloquent [profile] [path_to_file] [--id=] [--import]
  • [profile] - Name of a profile you have created in the config/eloquent-export.php file.
  • [path_to_file] - Export/Import file.
  • [--id=] - Primary ID ($primaryKey) of the root model data you want to export. Exp. if you are exporting a user then it would be the user's ID.
  • [--import] - Import the data, if not set then data will be exported from the database.

Example Usage

Export using the user_posts profile:, (*9)

php artisan export:eloquent user_posts /tmp/export/user_posts.json --id=34342
  • The output file is in JSON format

Import using the user_posts profile:, (*10)

php artisan export:eloquent user_posts /tmp/export/user_posts.json --import

The Versions

29/04 2018

dev-master

9999999-dev https://github.com/faulker/eloquent-export

A CLI tool for exporting and importing database data using Laravel's Eloquent models

  Sources   Download

MIT

The Requires

 

database laravel eloquent export backup import restore

01/05 2017

dev-bug/fix-path-create

dev-bug/fix-path-create https://github.com/faulker/eloquent-export

A CLI tool for exporting and importing database data using Laravel's Eloquent models

  Sources   Download

MIT

The Requires

 

database laravel eloquent export backup import restore

18/02 2017

v0.1.2

0.1.2.0 https://github.com/faulker/eloquent-export

A CLI tool for exporting and importing database data using Laravel's Eloquent models

  Sources   Download

MIT

The Requires

 

database laravel eloquent export backup import restore

18/02 2017

dev-bug/mutator-and-foreign-fix

dev-bug/mutator-and-foreign-fix https://github.com/faulker/eloquent-export

A CLI tool for exporting and importing database data using Laravel's Eloquent models

  Sources   Download

MIT

The Requires

 

database laravel eloquent export backup import restore

02/02 2017

v0.1.0

0.1.0.0 https://github.com/faulker/eloquent-export

A CLI tool for exporting and importing database data using Laravel's Eloquent models

  Sources   Download

MIT

The Requires

 

database laravel eloquent export backup import restore