Friday, 4 December 2020

Insert data using database seeder in laravel

Insert data using database seeder in laravel
php artisan make:seeder AdminsTableSeeder

<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class AdminsTableSeeder extends Seeder
{
    public function run()
    {
      DB::table('admins')->insert([
        'name'  => 'admin',
        'email' => 'admin@gmail.com',
        'password' => Hash::make('password'),
        ]);            
    }
}

composer dump-autoload

Run seeder
php artisan db:seed --class=AdminsTableSeeder

********************************************
To insert multiple record
public function run()
{
    DB::table('admins')->insert(array(
        array(
        'name' => "Steve",
        'email' => 'steve@gmail.com',
        'password' => Hash::make('secret'),
        ),
        array(
        'name' => "Laura",
        'email' => 'laura@gmail.com',
        'password' => Hash::make('secret'),
        )
    ));
}

********************************************
Insert data using factory in laravel
php artisan make:factory AdminsTableSeeder

<?php

use Faker\Generator as Faker;
use App\Models\Admin;
use Illuminate\Support\Str;
use Hash;

$factory->define(Admin::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'email' =>$faker->email,        
        'password' => Hash::make('password'),
    ];
});

php artisan make:seeder AdminsTableSeeder
<?php
use Illuminate\Database\Seeder;

class AdminsTableSeeder extends Seeder
{   
    public function run()
    {        
        factory(App\Models\Admin::class, 100)->create();
    }
}
php artisan make:seeder AdminsTableSeeder

No comments:

Post a Comment

Machine Learning - Potato Leaf Disease Prediction

Step 1: import numpy as np import pandas as pd import splitfolders import matplotlib.pyplot as plt import tensorflow as tf from tensorflow i...