Laravel utility to keep records synced between enviroments through source control
Laravel utility to keep records synchronized between environments through source control
config/data-sync.php
:'namespace' => '\\App\\Models\\',
php artisan data:export User --criteria=name --criteria=email --except=id
which generates
[
{
"_name": "Cameron Frye",
"properties->title": "Best Friend",
"phone_numbers->mobile": "555-555-5556",
"_email": "[email protected]",
}
]
composer require nullthoughts/laravel-data-sync:v3.1.x-dev
You can install this package via composer:
composer require nullthoughts/laravel-data-sync
Or add this line in your composer.json
, inside of the require
section:
{
"require": {
"nullthoughts/laravel-data-sync": "^1.0",
}
}
then run composer install
php artisan vendor:publish --provider="nullthoughts\LaravelDataSync\DataSyncBaseServiceProvider" --tag="data-sync-config"
to publish config file. Specify directory for sync data files (default is a new sync directory in the project root)php artisan data:sync
(or php artisan data:sync --model={model}
with the model flag to specify a model)If using Laravel Forge, you can have the data sync run automatically on deploy. Edit your deploy script in Site -> App to include:
if [ -f artisan ]
then
php artisan migrate --force
php artisan data:sync
fi
return [
'path' => base_path('sync'),
'order' => [
'Role',
'Supervisor',
]
];
[
{
"name": "Ferris Bueller",
"properties->title": "Leisure Consultant",
"phone_numbers->mobile": "555-555-5555",
"phone_numbers->office": "",
"_email": "[email protected]",
"department": {
"name": "Management",
"location": {
"name": "Chicago"
}
}
}
]
translates to...
User::updateOrCreate([
'email' => '[email protected]',
],[
'name' => 'Ferris Bueller',
'properties->title' => 'Leisure Consultant',
'phone_numbers->mobile' => '555-555-5555',
'department_id' => Department::where('name', 'Management')
->where('location_id', Location::where('name', 'Chicago')->first()->id)
->first()
->id,
]);
[
{
"_slug": "update-student-records"
},
{
"_slug": "borrow-ferrari"
},
{
"_slug": "destroy-ferrari"
}
]
translates to...
Role::updateOrCreate(['slug' => 'update-student-records']);
Role::updateOrCreate(['slug' => 'borrow-ferrari']);
Role::updateOrCreate(['slug' => 'destroy-ferrari']);
[
{
"_user": {
"email": "[email protected]"
},
"_role": {
"slug": "update-student-records"
}
},
{
"_user": {
"email": "[email protected]"
},
"_role": {
"slug": "borrow-ferrari"
}
},
{
"_user": {
"email": "[email protected]"
},
"_role": {
"slug": "destroy-ferrari"
}
}
]
translates to...
RoleUser::updateOrCreate([
'user_id' => User::where('email', '[email protected]')->first()->id,
'role_id' => Role::where('slug', 'update-student-records')->first()->id,
]);
RoleUser::updateOrCreate([
'user_id' => User::where('email', '[email protected]')->first()->id,
'role_id' => Role::where('slug', 'borrow-ferrari')->first()->id,
]);
RoleUser::updateOrCreate([
'user_id' => User::where('email', '[email protected]')->first()->id,
'role_id' => Role::where('slug', 'destroy-ferrari')->first()->id,
]);