Skip to content

Latest commit

 

History

History
25 lines (23 loc) · 906 Bytes

create-and-use-Seeders-in-laravel.md

File metadata and controls

25 lines (23 loc) · 906 Bytes

How to create and use Seeders in Laravel

  1. Create a seeder php artisan make:seeder seederName
  2. This creates a file seederName in database/seeds
  3. You can use a model or default database class. I'll show you both examples
  4. To use an existing model, try this approach
// assumes you've already added "use App\User" and "use Illuminate\Support\Facades\Hash;" at top of your file
$user = User::create([
          'name' => 'Saqib',
          'email' => 'saqib@saqib.com',
          'password' => Hash::make('password'),
          'level' => 1
        ]);
  1. If you don't have a model and want to run database inserts, try something like this
DB::table('users')->insert([
        'name' => 'Jon Snow',
        'email' => 'saqib@test.com',
        'password' => Hash::make('password')
      ]);
  1. Run php artisan db:seed or php artisan migrate:fresh --seed to run seeders