seed in codeigniter 4 code example

Example 1: codeigniter 4 database seed multiple

<?php

namespace App\Database\Seeds;

use \CodeIgniter\I18n\Time;

class PeopleSeeder extends \CodeIgniter\Database\Seeder
{
  public function run()
  {
    $data = [
      [
        'name'        => 'Andy',
        'address'     => '8733 Apple Street, 10',
        'created_at'  => Time::now(),
        'updated_at'  => Time::now()
      ],
      [
        'name'        => 'Buddy',
        'address'     => '643 Banana Street, 135',
        'created_at'  => Time::now(),
        'updated_at'  => Time::now()
      ],
      [
        'name'        => 'Anie',
        'address'     => '112 Lemon Street, 121',
        'created_at'  => Time::now(),
        'updated_at'  => Time::now()
      ]
    ];

    // Using Query Builder
    $this->db->table('peoples')->insertBatch($data);
  }
}

Example 2: codeigniter 4 db seed

<?php namespace App\Database\Seeds;

class SimpleSeeder extends \CodeIgniter\Database\Seeder
{
        public function run()
        {
                $data = [
                        'username' => 'darth',
                        'email'    => '[email protected]'
                ];

                // Simple Queries
                $this->db->query("INSERT INTO users (username, email) VALUES(:username:, :email:)",
                        $data
                );

                // Using Query Builder
                $this->db->table('users')->insert($data);
        }
}

Tags:

Php Example