Tạo SEED cho danh mục phẳng - Table Loại sản phẩm

Step 1: vào Start -> Run -> Cmd, trỏ đường dẫn đến thư mục project sunshine

  • Ví dụ: source nằm trong thư mục c:\xampp\htdocs\sunshine
cd c:\xampp\htdocs\sunshine

Step 2: sử dụng artisan để tạo SEED trong Laravel

  • Tạo SEED quản lý cấu trúc TABLE Loại
php artisan make:seeder LoaiTableSeeder
  • File LoaiTableSeeder sẽ được tạo trong thư mục `database\seeds\LoaiTableSeeder.php`

Step 3: viết code PHP, sử dụng các Hàm tương ứng trong Framework Laravel để tạo dữ liệu ban đầu cho Table Loại

Để xem về các hàm tương ứng, bạn xem thêm trên trang chủ của Laravel nhé: https://laravel.com/docs/master/seeding
<?php

use Illuminate\Database\Seeder;

class LoaiTableSeeder extends Seeder {
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run() {
        $list = [];

        $types = ["Hoa lẻ", "Phụ liệu", "Bó hoa", "Giỏ hoa", "Hoa hộp giấy", 
                       "Kệ hoa", "Vòng hoa", "Bình hoa", "Hoa hộp gỗ"];
        sort($types);

        $today = new DateTime('2019-01-01 08:00:00');

        for ($i=1; $i <= count($types); $i++) {
            array_push($list, [
                'l_ma'      => $i,
                'l_ten'     => $types[$i-1],
                'l_taoMoi'  => $today->format('Y-m-d H:i:s'),
                'l_capNhat' => $today->format('Y-m-d H:i:s')
            ]);
        }
        DB::table('cusc_loai')->insert($list);
    }
}

Step 4: thực thi SEED, cập nhật dữ liệu ban đầu cho table Loại

  • Thực thi câu lệnh sau:
php artisan db:seed --class=LoaiTableSeeder

Step 5: sử dụng HeidiSQL hay PhpMyAdmin để kiểm tra xem table Loại có dữ liệu ban đầu đúng với code trong file SEED hay không?

Source code tham khảo SEED table LOAIhttps://github.com/kellyfire611/sunshinenew/blob/master/database/seeds/LoaiTableSeeder.php