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/ */
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([ // 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