PHP code example of bfinlay / laravel-excel-seeder

1. Go to this page and download the library: Download bfinlay/laravel-excel-seeder 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/ */

    

bfinlay / laravel-excel-seeder example snippets


use Illuminate\Database\Seeder;
use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call([
            SpreadsheetSeeder::class,
        ]);
    }
}

use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;

class UsersTableSeeder extends SpreadsheetSeeder
{    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function settings(SpreadsheetSeederSettings $set)
    {
        // By default, the seeder will process all XLSX files in /database/seeds/*.xlsx (relative to Laravel project base path)
        
        // Example setting
        $set->worksheetTableMapping = ['Sheet1' => 'first_table', 'Sheet2' => 'second_table'];
    }
}

use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;

class UsersTableSeeder extends SpreadsheetSeeder
{    
    /**
     * Run the database seeds.
     *
     * @return void
     */
     
    public function settings(SpreadsheetSeederSettings $set)
    {
        // specify relative to Laravel project base path
        // feature directories specified
        $set->file = [
            '/database/seeds/feature1', 
            '/database/seeds/feature2',
            '/database/seeds/feature3', 
            ]; 
        
        // process all xlsx and csv files in paths specified above
        $set->extension = ['xlsx', 'csv'];        
    }
}

use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;

class UsersTableSeeder extends SpreadsheetSeeder
{    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function settings(SpreadsheetSeederSettings $set)
    {
        // specify relative to Laravel project base path
        $set->file = [
            '/database/seeds/file1.xlsx', 
            '/database/seeds/file2.xlsx',
            '/database/seeds/seed*.xlsx', 
            '/database/seeds/*.csv']; 
    }
}

use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;

class UsersTableSeeder extends SpreadsheetSeeder
{    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function settings(SpreadsheetSeederSettings $set)
    {
        // specify relative to Laravel project base path
        $set->file =
            (new Finder)
                ->in(base_path() . '/database/seeds/')
                ->name('*.xlsx')
                ->notName('*customers*')
                ->sortByName();
    }
}

use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;

class SalesTableSeeder extends SpreadsheetSeeder
{    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function settings(SpreadsheetSeederSettings $set)
    {
        $set->file = '/database/seeds/sales.xlsx';
        if (App::environment('local'))
            $set->limit = 10000;
    }
}

['email' => function ($value) {
     return strtolower($value);
}];

use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;

class UsersTableSeeder extends SpreadsheetSeeder
{    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function settings(SpreadsheetSeederSettings $set)
    {
        // specify relative to Laravel project base path
        // specify filename that is automatically dumped from an external process
        $set->file = '/database/seeds/autodump01234456789.xlsx';  // note: could alternatively be a csv
        
        // specify the table this is loaded into
        $set->tablename = 'sales';
        
        // in this example, table truncation also needs to be disabled so previous sales records are not deleted
        $set->truncate = false;
    }
}

use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;

class UsersTableSeeder extends SpreadsheetSeeder
{    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function settings(SpreadsheetSeederSettings $set)
    {
        // specify the table this is loaded into
        $set->worksheetTableMapping = [
            'first_table_name_abbreviated' => 'really_rather_very_super_long_first_table_name', 
            'second_table_name_abbreviated' => 'really_rather_very_super_long_second_table_name'
            ];
    }
}

use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;

class UsersTableSeeder extends SpreadsheetSeeder
{    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function settings(SpreadsheetSeederSettings $set)
    {
        $set->file = '/database/seeds/csvs/users.csv';
        $set->tablename = 'email_users';
        $set->timestamps = '1970-01-01 00:00:00';
    }
}

use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;

class UsersTableSeeder extends SpreadsheetSeeder
{    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function settings(SpreadsheetSeederSettings $set)
    {
        $set->file = '/database/seeds/users.xlsx';
        $set->mapping = ['id', 'firstname', 'lastname'];
        $set->header = FALSE;
    }
}

use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;

class UsersTableSeeder extends SpreadsheetSeeder
{    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function settings(SpreadsheetSeederSettings $set)
    {
        $set->file = '/database/seeds/csvs/users.csv';
        $set->aliases = ['csvColumnName' => 'table_column_name', 'foo' => 'bar'];
        $set->defaults = ['created_by' => 'seeder', 'updated_by' => 'seeder'];
    }
}

use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;

class UsersTableSeeder extends SpreadsheetSeeder
{    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function settings(SpreadsheetSeederSettings $set)
    {
        $set->file = '/database/seeds/users.xlsx';
        $set->skipper = 'skip:';
    }
}

use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;

class UsersTableSeeder extends SpreadsheetSeeder
{    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function settings(SpreadsheetSeederSettings $set)
    {
        $set->file = '/database/seeds/users.xlsx';
        $set->validate = [ 'name'              => '

use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;

class UsersTableSeeder extends SpreadsheetSeeder
{    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function settings(SpreadsheetSeederSettings $set)
    {
        $set->file = '/database/seeds/users.xlsx';
        $set->hashable = ['password'];
    }
}

use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;

class UsersTableSeeder extends SpreadsheetSeeder
{    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function settings(SpreadsheetSeederSettings $set)
    {
        $set->file = '/database/seeds/users.xlsx';
        $set->inputEncodings = ['UTF-8', 'ISO-8859-1'];
        $set->outputEncoding = 'UTF-8';
    }
}

php artisan xl:seed --sheet=users --sheet=posts 

php artisan xl:seed --class=MySpreadsheetSeederClass --sheet=users --sheet=posts

php artisan xl:seed --class=# --sheet=users --sheet=posts

php artisan xl:seed # users posts