PHP code example of guava / laravel-populator

1. Go to this page and download the library: Download guava/laravel-populator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

guava / laravel-populator example snippets


return [
    'tracking' => false,
    'population_model' => '\Guava\LaravelPopulator\Models\Population',
];


return new class extends Migration {

    public function up() {
        Populator::make('initial') // // bundles are located in /database/populators/initial/
            ->environments(['local', 'testing'])
            ->bundles([
                Bundle::make(User::class)
                    ->mutate('password', fn($value) => Hash::make($value))
                    ->records([
                        'admin' => [
                            'name' => 'Administrator',
                            'email' => '[email protected]',
                            'password' => 'my-strong-password',
                        ],
                    ]),
                    
                Bundle::make(Tag::class, 'my-tags'), // records are located in /database/populators/initial/my-tags/
                    
                Bundle::make(Post::class) // records are located in /database/populators/initial/post/
                    ->generate('slug', fn(array $attributes) => Str::slug($attributes['name'])),
               
                Bundle::make(Permission::class) // records are located in /database/populators/initial/permission/
                    ->default('guard_name', 'web'),

                Bundle::make(Role::class) // records are located in /database/populators/initial/role/
                    ->default('guard_name', 'web'),
                    
            ]);
    }
    
}



return [
    'name' => 'Example post',
    'content' => 'Lorem ipsum dolor sit amet',

    'author' => 'admin', // could also be ID or specific column:value, like email:[email protected]
    'tags' => ['Technology', 'Design', 'Off-topic'],
];

Populator::make('v1')
    ->bundles([
        Bundle::make(User::class),
    ])
    ->call()


return [
    'name' => 'John Doe',
    'email' => '[email protected]',
    'password' => 'my-strong-password',
];

Populator::make('v1')
    ->bundles([ // Your bundles here])
    ->call()

Populator::make('v1')
    ->bundles([//your bundles to reverse or leave blank for all bundles in the populator])
    ->rollback()


Populator::make('initial')
    ->bundles([
        Bundle::make(User::class),
        Bundle::make(Post::class),
    ])
    ->call();
//User and Post entries now exist 

Populator::make('initial')
    ->bundles([Bundle::make(Post::class)])
    ->rollback();
//Post entries were removed

Populator::make('v1')
    ->environments(['local'])
    ...
    ->call()

Bundle::make(Model::class, 'optional-name'),

Bundle::make(User::class)
    ->environments(['production'])

Bundle::make(User::class)
    ->mutate('password', fn($value) => Hash::make($value))

Bundle::make(Permission::class)
    ->default('guard_name', 'web'),

Bundle::make(Post::class)
    ->generate('slug', fn($attributes) => Str::slug($attributes['name']))

...
Bundle::make(User::class)
    ->mutate('password', fn($value) => Hash::make($value))
    ->records([
        'admin' => [
            'name' => 'Administrator',
            'email' => '[email protected]',
            'password' => 'admin123',
        ],
    ]);
...

 Bundle::make(User::class)
    ->performInsertUsing(function (array $data, Bundle $bundle) {
        return $bundle->model::updateOrCreate(
            Arr::only($data, ['email']),
            Arr::except($data, ['email'])
        )->getKey();
    });


return [
    'street' => 'Example Street',
    'city' => 'Example City',
    'state' => 'Example State',
    'zip' => '12345',
    'user' => 'admin',
];

return [
    ...
    'user' => 1,
];

return [
    ...
    'user' => 'email:[email protected]',
];


return [
    'name' => 'Webmaster',
    'email' => '[email protected]',
    'password' => 'my-strong-password',
    'address' => [
        'street' => 'Example street',
        'city' => 'Example city',
        'zip' => '12345',
        'state' => 'Example State',
    ]
];


return [
    'name' => 'Example post',
    'slug' => 'example-post',
    'author' => 'john-doe',
];


return [
    'name' => 'Example post',
    'slug' => 'example-post',
    'author' => 1,
    'tags' => ['technology', 'design', 'off-topic']
];


return [
    'name' => 'A useful comment',
    'author' => 1,
    'post' => ['example-post', Post::class],
];


return [
    'name' => 'Example post',
    'slug' => 'example-post',
    'author' => 1,
    'tags' => ['technology', 'design', 'off-topic'],
    'likes' => [
        [
            'user' => '[email protected]',
        ],
        [
            'user' => '[email protected]',
        ]
    ]
];

\Guava\LaravelPopulator\Models\Population::first()
->populatable; //provides access to the model that was inserted by Laravel populator.

class User extends Model implements TracksPopulatedEntries
{
    use HasPopulation;
}

return [
    'tracking' => true,
    //...other options
];

public function boot() {
    \Guava\LaravelPopulator\Facades\Feature::enableTrackingFeature();
}

class User extends Model implements TracksPopulatedEntries
{
}

return [
    'population_model' => SomeModel::class,
    //...other options
];

public function boot() {
    \Guava\LaravelPopulator\Facades\Feature::customPopulationModel(SomeModel::class);
}
bash
php artisan vendor:publish --provider 'Guava\LaravelPopulator\PopulatorServiceProvider' --tag 'migrations'
php artisan migrate
bash
php artisan vendor:publish --provider 'Guava\LaravelPopulator\PopulatorServiceProvider' --tag 'config'
bash
php artisan make:migration populate_initial_data