Skip to content

Instantly share code, notes, and snippets.

@francoism90
Last active December 14, 2017 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francoism90/3b63d504d45e8211fa9906a061c32463 to your computer and use it in GitHub Desktop.
Save francoism90/3b63d504d45e8211fa9906a061c32463 to your computer and use it in GitHub Desktop.
Laravel Seeder Generator KISS
<?php
/*
* Your settings here
*/
define('DB_DSN', 'mysql:host=localhost;dbname=mydb;charset=UTF8');
define('DB_USER', 'myuser');
define('DB_PASS', 'mypass');
define('DB_TABLE', 'mytable');
define('DB_OPTIONS', [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
]);
require('helper.php');
/*
* Script starts here
*/
try {
$db = new PDO(DB_DSN, DB_USER, DB_PASS, DB_OPTIONS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
exit('PDO: ' . $e->getMessage());
}
$sth = $db->prepare(sprintf('SELECT * FROM %s', DB_TABLE));
$sth->execute();
foreach($sth->fetchAll() as $row) {
// Modify this to your own needs
$slug = str_slug($row['title']);
$title = addslashes($row['title']);
echo '[' . PHP_EOL;
echo "\t'id' => {$row['id']}," . PHP_EOL;
echo "\t'name' => '{$title}'," . PHP_EOL;
echo "\t'slug' => '{$slug}'," . PHP_EOL;
echo '],' . PHP_EOL . PHP_EOL;
}
<?php
// Copy contents from https://github.com/rappasoft/laravel-helpers or include your own
@francoism90
Copy link
Author

francoism90 commented Sep 9, 2017

$ php generator.php > seeds

/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{
    DB::table('import-table')->insert([
        // generator contents
    ]);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment