Laravel – Database – Phần 2

Author: - Posted: 08/03/20 - Update: 08/03/20

database

III. Tạo model

1. Tạo file model

- Dùng terminal, chạy từng lệnh sau:

- Vào trong app/.. đã thấy có 3 file php tương ứng

2. Định nghĩa từng Model

- Giả sử với file Product.php trong app/..

3. Định nghĩa factory

- Vào /database/factories/UserFactory.php, thêm vào cuối như sau:

4. Định nghĩa seeds

- Tạo file CategorySeeder.php trong seeds bằng terminal

- Định nghĩa file /database/seeds/CategorySeeder.php

- Định nghĩa file /database/seeds/DatabaseSeeder.php

5. Khởi chạy seeds

- Dùng terminal

- Khi đã tạo nhiều dữ liệu fake rồi, muốn refresh lại seed lại thì dùng lệnh sau:

- Quay lại database, trong bảng Category, đã tạo ra được 100 dữ liệu fake 😀