Laravel Seeding Inserting data


Example

There are several ways to insert data:

Using the DB Facade

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

Via Instantiating a Model

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

Using the create method

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

Using factory

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

Seeding && deleting old data and reseting auto-increment

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.