Last active
December 14, 2017 13:27
-
-
Save francoism90/3b63d504d45e8211fa9906a061c32463 to your computer and use it in GitHub Desktop.
Laravel Seeder Generator KISS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Copy contents from https://github.com/rappasoft/laravel-helpers or include your own |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ php generator.php > seeds