PHP code example of justinkekeocha / database-dump

1. Go to this page and download the library: Download justinkekeocha/database-dump 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/ */

    

justinkekeocha / database-dump example snippets


return [

    /*
     *  Enable or disable the package.
    */
    'enable' => true,

    /*
     *  Set the folder generated dumps should be save in.
    */

    'folder' => database_path('dumps/'),

    /*
     *  Set the chunk length of data to be processed at once.
    */
    'chunk_length' => 5000,

    /*
    *  Set the maximum stream length of data to be processed at once.
    *  This is the maximum size a row in a table is expected to have in your database
    *  This is set to a reasonable default of 1MB
    *  If your database rows are larger than this, you may want to increase this value.
    *  Read more: https://www.php.net/manual/en/function.stream-get-line.php
    */

    'stream_length' => (2 * 1024 * 1024),
];

# Dump database data before running migrations
php artisan migrate:fresh

# Dump database data
php artisan database:dump


# database/seeders/DatabaseSeeder.php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Justinkekeocha\DatabaseDump\Facades\DatabaseDump;
use Database\Seeders\UserSeeder;


class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {

         $databaseDump = DatabaseDump::getLatestDump("save/2024_04_14_233109.json");

        $this->command->outputComponents()->info("Using dump:  $databaseDump->filePath");


        $this->call([
            UserSeeder::class,
        ], parameters: compact('databaseDump'));
    }
}

# database/seeders/UserSeeder.php

namespace Database\Seeders;

use App\Models\User;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run($databaseDump): void
    {
        $databaseDump->seed(User::class);

        //You can also use table name instead of model.

        $databaseDump->seed('users');
    }
}


# database/seeders/CountrySeeder.php

namespace Database\Seeders;

use App\Models\Country;

class CountrySeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run($databaseDump): void
    {
        $databaseDump->seed(Country::class, formatRowCallback: function ($row) {
                //331.69 ms
                return [
                    'id' => $row['id'],
                    'name' => $row['name'],
                    'code' => 22
                ];

                //OR

                //338.95 ms
                $changes = [
                    'code' => '22'
                ];

                return  collect($row)->only(['id', 'name'])->merge($changes)->all();
    });
    }
}



use Justinkekeocha\DatabaseDump\Facades\DatabaseDump;

//Get dump by position in array of directory listing
//Array starts from latest dump file in specified config('database-dump.folder')
DatabaseDump::getDump(1); //Get second dump in the array.

//Get dump by dump file name
DatabaseDump::getDump("2024_01_08_165939.json");

//Get the latest dump
DatabaseDump::getLatestDump();



use Justinkekeocha\DatabaseDump\Facades\DatabaseDump;
use App\Models\Country;
use App\Models\Timezone;
use App\Models\User;

DatabaseDump::getLatestDump()->seed(User::class);

//You can seed multiple tables at once.
DatabaseDump::getLatestDump()->seed(Country::class)
->seed(Timezone::class)
->seed(User::class);



use Justinkekeocha\DatabaseDump\Facades\DatabaseDump;
use App\Models\Country;
use App\Models\Timezone;
use App\Models\User;

//Whole file will be read 3 times
DatabaseDump::getLatestDump()->seed(Country::class);
DatabaseDump::getLatestDump()->seed(Timezone::class);
DatabaseDump::getLatestDump()->seed(User::class);


//Whole file will be read only once.
DatabaseDump::getLatestDump()->seed(Country::class)
->seed(Timezone::class)
->seed(User::class);

bash
php artisan vendor:publish --tag="database-dump-config"