Skip to content

Instantly share code, notes, and snippets.

@dwoodard
Last active March 3, 2019 18:32
Show Gist options
  • Save dwoodard/a643869a6ea24921d9119bb81804b2d9 to your computer and use it in GitHub Desktop.
Save dwoodard/a643869a6ea24921d9119bb81804b2d9 to your computer and use it in GitHub Desktop.
Laravel Creating MVC with factories and Seeds in the fewest key strokes

Laravel Creating [M]V[C] with factories and Seeds in the fewest key strokes

Having the Schema Ready would be great place to start!

Dog
- id
- name
- birthday
- breed ()
- gender *(m/f)*

php artisan make:model has shortcuts for making 4 files

php artisan make:model Dogs -a
  • Model - Dogs.php
  • Resource Controller - DogController.php
  • Migration - 2019_01_01_000000_create_dogs_table.php $table->string('name');
  • Factory - DogFactory.php 'name' => $faker->name,
php artisan make:seeder DogSeeder

Seeder - DogSeeder.php

        factory(App\Dog::class, 50)
            ->create()
            ->each(function ($dog) {
                $dog->posts()->save(factory(App\Post::class)->make());
            });

DogSeeder.php will need to be added to DatabaseSeeder.php

public function run()
    {
        // $this->call(UsersTableSeeder::class);
         $this->call(DogSeeder::class);
    }

You might need to run composer dump-autoload

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