VANHIEP.NET - Làm web giá rẻ - Thiết Kế Website - Thiết Kế Ứng Dụng Mobile

Tạo dữ liệu mẫu bằng seeder trong laravel

sau khi tạo bảng xong ta có thể tạo dữ liệu mẫu bằng các bước sau

* Bước 1 chạy cậu lệnh tạo seeder : php artisan make:seeder TenBang

* Bước 2 vào thư mục seeds mở file vừa tạo để thêm dữ liệu mẫu vào

Ví dụ: 

DB::table('category')->insert([

            ['id'=>1,'name'=>'Nam','slug'=>'nam','parent'=>0],

            ['id'=>2,'name'=>'Nữ','slug'=>'nu','parent'=>0],

            ['id'=>3,'name'=>'Quần Nam','slug'=>'quan-nam','parent'=>1],

            ['id'=>4,'name'=>'Quần Nữ','slug'=>'quan-nu','parent'=>2],

            ['id'=>5,'name'=>'Áo Nam','slug'=>'ao-nam','parent'=>1],

            ['id'=>6,'name'=>'Áo Nữ','slug'=>'ao-nu','parent'=>2],

        ]);

* Bước 3 vào thư mục seeds mở file DatabaseSeeder.php để setup bảng cần tạo dữ liệu mẫu

Ví dụ : 

        $this->call(category::class);

        $this->call(product::class);

        $this->call(users::class);

        $this->call(order::class);

* Bước 4 chạy lệnh sau : php artisan db:seed

Chúc bạn thành công !