Laravel

Laravel TutorialChange default routing behaviour in Laravel 5.2.31 +CustomException class in LaravelDeploy Laravel 5 App on Shared Hosting on Linux ServerLaravel ArtisanLaravel AuthenticationLaravel AuthorizationLaravel Blade TemplatesLaravel CashierLaravel CollectionsLaravel Common Issues & Quick FixesLaravel ConstantsLaravel ControllersLaravel Cron basicsLaravel Cross Domain RequestLaravel Custom Helper functionLaravel DatabaseLaravel Database MigrationsLaravel Database SeedingLaravel Directory StructureLaravel DockerLaravel EloquentLaravel Eloquent : RelationshipLaravel Eloquent: Accessors & MutatorsLaravel Eloquent: ModelLaravel Error HandlingLaravel Events and ListenersLaravel Filesystem / Cloud StorageLaravel Form Request(s)Laravel Getting started with laravel-5.3Laravel HelpersLaravel HTML and Form BuilderLaravel InstallationLaravel Installation GuideLaravel Introduction to laravel-5.2Laravel Introduction to laravel-5.3Laravel lumen frameworkLaravel Macros in Eloquent RelationshipLaravel MailLaravel MiddlewareLaravel ObserverLaravel PackagesLaravel PaginationLaravel Permissions for storageLaravel PoliciesLaravel QueuesLaravel RequestsLaravel Route Model BindingLaravel RoutingLaravel SeedingLaravel ServicesLaravel SocialiteLaravel Task SchedulingLaravel TestingLaravel Token Mismatch Error in AJAXLaravel use fields aliases in EloquentLaravel Useful linksLaravel ValetLaravel ValidationMultiple DB Connections in LaravelNaming Files when uploading with Laravel on WindowsRemove public from URL in laravelSparkpost integration with Laravel 5.4



Laravel Seeding

From WikiOD

Remarks[edit | edit source]

Database seeding allows you to insert data, general test data into your database. By default there is a DatabaseSeeder class under database/seeds.

Running seeders can be done with

php artisan db:seed

Or if you only want to process a single class

php artisan db:seed --class=TestSeederClass

As with all artisan commands, you have access to a wide array of methods which can be found in the api documentation

Creating a Seeder[edit | edit source]

To create seeders, you may use the make:seeder Artisan command. All seeders generated will be placed in the database/seeds directory.

$ php artisan make:seeder MoviesTableSeeder

Generated seeders will contain one method: run. You may insert data into your database in this method.

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class MoviesTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        App\Movie::create([
            'name' => 'A New Hope',
            'year' => '1977'
        ]);

        App\Movie::create([
            'name' => 'The Empire Strikes Back',
            'year' => '1980'
        ]);
    }
}

You will generally want to call all your seeders inside the DatabaseSeeder class.

Once you're done writing the seeders, use the db:seed command. This will run DatabaseSeeder's run function.

$ php artisan db:seed

You may also specify to run a specific seeder class to run individually using the --class option.

$ php artisan db:seed --class=UserSeeder

If you want to rollback and rerun all migrations, and then reseed:

$ php artisan migrate:refresh --seed

The migrate:refresh --seed command is a shortcut to these 3 commands:

$ php artisan migrate:reset     # rollback all migrations
$ php artisan migrate           # run migrations
$ php artisan db:seed           # run seeders

Inserting data[edit | edit source]

There are several ways to insert data:

Using the DB Facade[edit | edit source]

public function run()
{
    DB::table('users')
        ->insert([
            'name' => 'Taylor',
            'age'  => 21
        ]);
}

Via Instantiating a Model[edit | edit source]

public function run()
{
    $user = new User;
    $user->name = 'Taylor';
    $user->save();
}

Using the create method[edit | edit source]

public function run()
{
    User::create([
        'name' => 'Taylor',
        'age'  => 21
    ]);
}

Using factory[edit | edit source]

public function run()
{
    factory(App\User::class, 10)->create();
}

Seeding && deleting old data and reseting auto-increment[edit | edit source]

public function run()
{
    DB::table('users')->delete();
    DB::unprepared('ALTER TABLE users AUTO_INCREMENT=1;');
    factory(App\User::class, 200)->create();
}

See the Persisting example for more information on inserting/updating data.

Calling other seeders[edit | edit source]

Within your DatabaseSeeder class you are able to call other seeders

$this->call(TestSeeder::class)

This allows you to keep one file where you can easily find your seeders. Keep in mind that you need to pay attention to the order of your calls regarding foreign key constraints. You can't reference a table that doesn't exist yet.

Safe reseeding[edit | edit source]

You may want to re-seed your database without affecting your previously created seeds. For this purpose, you can use firstOrCreate in your seeder:

EmployeeType::firstOrCreate([
    'type' => 'manager',
]);

Then you can seed the database:

php artisan db:seed

Later, if you want to add another type of employee, you can just add that new one in the same file:

EmployeeType::firstOrCreate([
    'type' => 'manager',
]);
EmployeeType::firstOrCreate([
    'type' => 'secretary',
]);

And seed your database again with no problems:

php artisan db:seed

Notice in the first call you are retrieving the record but doing nothing with it.

Credit:Stack_Overflow_Documentation