├── README.md ├── composer.json ├── preview.gif └── src ├── Commands └── SeedsCommand.php └── SeedsServiceProvider.php /README.md: -------------------------------------------------------------------------------- 1 | # Seeds for Laravel by Laracademy 2 | 3 | ![Latest Stable Version](https://poser.pugx.org/laracademy/seeds/v/stable) [![Total Downloads](https://poser.pugx.org/laracademy/seeds/downloads)](https://packagist.org/packages/laracademy/seeds) [![Latest Unstable Version](https://poser.pugx.org/laracademy/seeds/v/unstable)](https://packagist.org/packages/laracademy/seeds) [![License](https://poser.pugx.org/laracademy/seeds/license)](https://packagist.org/packages/laracademy/seeds) 4 | 5 | This package adds a new `artisan` command which offers a better way to seed your database: You can select the seeder from a selection and during running you will get feedback on what is being done. 6 | 7 | ## Getting Started 8 | 9 | The easiest way to get started is to install the package using `composer`. You can do so by running the following command. 10 | 11 | ```bash 12 | composer require laracademy/seeds 13 | ``` 14 | 15 | That's it! Well, as long as you are using `Laravel 5.5 or higher`. If you are using an older version of Laravel please see below. 16 | 17 | ## Usage 18 | 19 | To use the package you just need to run the `artisan` command and follow the prompts on the screen. 20 | 21 | ```bash 22 | php artisan seeds 23 | ``` 24 | 25 | ### Preview 26 | ![](preview.gif) 27 | 28 | ### Laravel 5.4 and lower 29 | 30 | You will need to add the package into your service provider array found in `config/app.php`. 31 | 32 | ```php 33 | providers = [ 34 | // listing of providers 35 | Laracademy\Commands\SeedsServiceProvider::class, 36 | // rest of providers 37 | ]; 38 | ``` 39 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laracademy/seeds", 3 | "description": "This package helps with database seeding.", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "Michael McMullen", 8 | "email": "michael.mcmullen@laracademy.co" 9 | } 10 | ], 11 | "require": {}, 12 | "minimum-stability": "dev", 13 | "autoload": { 14 | "psr-4": { 15 | "Laracademy\\": "src/" 16 | } 17 | }, 18 | "extra": { 19 | "laravel": { 20 | "providers": [ 21 | "Laracademy\\Commands\\SeedsServiceProvider" 22 | ] 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /preview.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laracademy/seeds/5733ec5a94870436b04ac7187d422d66c0abc4c4/preview.gif -------------------------------------------------------------------------------- /src/Commands/SeedsCommand.php: -------------------------------------------------------------------------------- 1 | reject(function($record) { 43 | $filename = collect(explode("/", $record))->last(); 44 | return $filename == 'DatabaseSeeder.php'; 45 | }); 46 | // clean up path 47 | $files = $files->map(function($record) use($path) { 48 | return str_replace("{$path}/", '', $record); 49 | }); 50 | // add exit 51 | $files = $files->prepend('Exit'); 52 | // reverse the listing 53 | $index = 0; 54 | $files = $files->flatMap(function($record) use (&$index) { 55 | return [ 56 | $record => $index++, 57 | ]; 58 | }); 59 | while(! $willExit) { 60 | // show list 61 | $answer = $this->choice('Please choose a database seeder', $files->toArray()); 62 | if($answer == 'Exit') { 63 | $willExit = true; 64 | } else { 65 | // find the answer in the listing 66 | $file = $files->reject(function($index, $value) use ($answer) { 67 | return $value != $answer; 68 | })->keys()->first(); 69 | // grab the class name 70 | $class = collect($this->file_get_php_classes("{$path}/{$file}"))->first(); 71 | $this->info('Running: '. $class); 72 | sleep(1); 73 | $this->call('db:seed', [ 74 | '--class' => $class, 75 | ]); 76 | } 77 | } 78 | } 79 | function file_get_php_classes($filepath) 80 | { 81 | $php_code = file_get_contents($filepath); 82 | $classes = $this->get_php_classes($php_code); 83 | return $classes; 84 | } 85 | function get_php_classes($php_code) 86 | { 87 | $classes = array(); 88 | $tokens = token_get_all($php_code); 89 | $count = count($tokens); 90 | for ($i = 2; $i < $count; $i++) { 91 | if ($tokens[$i - 2][0] == T_CLASS 92 | && $tokens[$i - 1][0] == T_WHITESPACE 93 | && $tokens[$i][0] == T_STRING) { 94 | $class_name = $tokens[$i][1]; 95 | $classes[] = $class_name; 96 | } 97 | } 98 | return $classes; 99 | } 100 | 101 | } -------------------------------------------------------------------------------- /src/SeedsServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->singleton('command.laracademy.seeds', function($app) { 24 | return $app['Laracademy\Commands\SeedsCommand']; 25 | }); 26 | 27 | $this->commands('command.laracademy.seeds'); 28 | } 29 | 30 | } --------------------------------------------------------------------------------