Đa đối tượng php

Laravel bao gồm Eloquent, một trình ánh xạ quan hệ đối tượng (ORM) giúp tương tác với cơ sở dữ liệu của bạn trở nên thú vị. Khi sử dụng Eloquent, mỗi bảng cơ sở dữ liệu có một "Mô hình" tương ứng được sử dụng để tương tác với bảng đó. Ngoài việc truy xuất các bản ghi từ bảng cơ sở dữ liệu, các mô hình Eloquent còn cho phép bạn chèn, cập nhật và xóa các bản ghi khỏi bảng.

Lưu ý
Trước khi bắt đầu, hãy đảm bảo định cấu hình kết nối cơ sở dữ liệu trong tệp cấu hình

php artisan make:model Flight --migration

01 của ứng dụng của bạn. Để biết thêm thông tin về cách định cấu hình cơ sở dữ liệu của bạn, hãy xem tài liệu cấu hình cơ sở dữ liệu.

Trại khởi động Laravel

Nếu bạn chưa quen với Laravel, hãy thoải mái tham gia Laravel Bootcamp. Laravel Bootcamp sẽ hướng dẫn bạn cách xây dựng ứng dụng Laravel đầu tiên của bạn bằng Eloquent. Đó là một cách tuyệt vời để tham quan mọi thứ mà Laravel và Eloquent cung cấp

Tạo các lớp mô hình

Để bắt đầu, hãy tạo một mô hình Eloquent. Model thường nằm trong thư mục

php artisan make:model Flight --migration

02 và mở rộng lớp

php artisan make:model Flight --migration

03. Bạn có thể sử dụng lệnh Artisan

php artisan make:model Flight --migration

04 để tạo một mô hình mới

php artisan make:model Flight

Nếu bạn muốn tạo di chuyển cơ sở dữ liệu khi tạo mô hình, bạn có thể sử dụng tùy chọn

php artisan make:model Flight --migration

05 hoặc

php artisan make:model Flight --migration

06

php artisan make:model Flight --migration

Bạn có thể tạo nhiều loại lớp khác nhau khi tạo mô hình, chẳng hạn như nhà máy, trình gieo hạt, chính sách, bộ điều khiển và yêu cầu biểu mẫu. Ngoài ra, các tùy chọn này có thể được kết hợp để tạo nhiều lớp cùng một lúc

________số 8

kiểm tra mô hình

Đôi khi có thể khó xác định tất cả các thuộc tính và mối quan hệ có sẵn của một mô hình chỉ bằng cách lướt qua mã của nó. Thay vào đó, hãy thử lệnh

php artisan make:model Flight --migration

07 Artisan, lệnh này cung cấp tổng quan thuận tiện về tất cả các thuộc tính và quan hệ của mô hình

php artisan make:model Flight --migration

0

Công ước mô hình hùng hồn

Các mô hình được tạo bởi lệnh

php artisan make:model Flight --migration

04 sẽ được đặt trong thư mục

php artisan make:model Flight --migration

09. Hãy kiểm tra một lớp mô hình cơ bản và thảo luận về một số quy ước chính của Eloquent

php artisan make:model Flight --migration

3

tên bảng

Sau khi xem qua ví dụ trên, bạn có thể nhận thấy rằng chúng tôi đã không cho Eloquent biết bảng cơ sở dữ liệu nào tương ứng với mô hình

php artisan make:model Flight --migration

10 của chúng tôi. Theo quy ước, "trường hợp rắn", tên số nhiều của lớp sẽ được sử dụng làm tên bảng trừ khi một tên khác được chỉ định rõ ràng. Vì vậy, trong trường hợp này, Eloquent sẽ giả sử mô hình

php artisan make:model Flight --migration

10 lưu trữ các bản ghi trong bảng

php artisan make:model Flight --migration

12, trong khi mô hình

php artisan make:model Flight --migration

13 sẽ lưu trữ các bản ghi trong bảng

php artisan make:model Flight --migration

14

Nếu bảng cơ sở dữ liệu tương ứng của mô hình của bạn không phù hợp với quy ước này, bạn có thể chỉ định tên bảng của mô hình theo cách thủ công bằng cách xác định thuộc tính

php artisan make:model Flight --migration

15 trên mô hình

php artisan make:model Flight --migration

0

Khóa chính

Eloquent cũng sẽ giả định rằng bảng cơ sở dữ liệu tương ứng của mỗi mô hình có một cột khóa chính có tên là

php artisan make:model Flight --migration

16. Nếu cần, bạn có thể xác định thuộc tính

php artisan make:model Flight --migration

17 được bảo vệ trên mô hình của mình để chỉ định một cột khác đóng vai trò là khóa chính của mô hình

php artisan make:model Flight --migration

3

Ngoài ra, Eloquent giả định rằng khóa chính là một giá trị số nguyên tăng dần, điều đó có nghĩa là Eloquent sẽ tự động chuyển khóa chính thành một số nguyên. Nếu bạn muốn sử dụng khóa chính không tăng dần hoặc không phải là số, bạn phải xác định thuộc tính

php artisan make:model Flight --migration

18 công khai trên mô hình của mình được đặt thành

php artisan make:model Flight --migration

19

php artisan make:model Flight --migration

6

Nếu khóa chính của mô hình của bạn không phải là số nguyên, thì bạn nên xác định thuộc tính được bảo vệ

php artisan make:model Flight --migration

20 trên mô hình của mình. Thuộc tính này phải có giá trị là

php artisan make:model Flight --migration

21

php artisan make:model Flight --migration

9

Khóa chính "tổng hợp"

Eloquent yêu cầu mỗi mô hình phải có ít nhất một "ID" nhận dạng duy nhất có thể đóng vai trò là khóa chính của nó. Các khóa chính "tổng hợp" không được hỗ trợ bởi các mô hình Eloquent. Tuy nhiên, bạn có thể tự do thêm các chỉ mục duy nhất, nhiều cột bổ sung vào các bảng cơ sở dữ liệu của mình ngoài khóa chính xác định duy nhất của bảng

Khóa UUID & ULID

Thay vì sử dụng số nguyên tăng tự động làm khóa chính của mô hình Eloquent, bạn có thể chọn sử dụng UUID thay thế. UUID là số nhận dạng chữ và số duy nhất trên toàn cầu dài 36 ký tự

Nếu bạn muốn một mô hình sử dụng khóa UUID thay vì khóa số nguyên tăng tự động, bạn có thể sử dụng đặc điểm

php artisan make:model Flight --migration

22 trên mô hình. Tất nhiên, bạn nên đảm bảo rằng mô hình có cột khóa chính tương đương UUID

php artisan make:model Flight --migration

1

Theo mặc định, Đặc điểm

php artisan make:model Flight --migration

23 sẽ tạo UUID "được sắp xếp" cho các mô hình của bạn. Các UUID này hiệu quả hơn cho việc lưu trữ cơ sở dữ liệu được lập chỉ mục vì chúng có thể được sắp xếp theo từ điển

Bạn có thể ghi đè quá trình tạo UUID cho một mô hình nhất định bằng cách xác định phương thức

php artisan make:model Flight --migration

24 trên mô hình. Ngoài ra, bạn có thể chỉ định cột nào sẽ nhận UUID bằng cách xác định phương thức

php artisan make:model Flight --migration

25 trên mô hình

php artisan make:model Flight --migration

0

Nếu muốn, bạn có thể chọn sử dụng "ULID" thay vì UUID. ULID tương tự như UUID; . Giống như các UUID được sắp xếp, các ULID có thể sắp xếp theo thứ tự từ điển để lập chỉ mục cơ sở dữ liệu hiệu quả. Để sử dụng ULID, bạn nên sử dụng đặc điểm

php artisan make:model Flight --migration

26 trên mô hình của mình. Bạn cũng nên đảm bảo rằng mô hình có cột khóa chính tương đương ULID

php artisan make:model Flight --migration

1

Dấu thời gian

Theo mặc định, Eloquent mong đợi các cột

php artisan make:model Flight --migration

27 và

php artisan make:model Flight --migration

28 tồn tại trên bảng cơ sở dữ liệu tương ứng của mô hình của bạn. Eloquent sẽ tự động đặt giá trị của các cột này khi các mô hình được tạo hoặc cập nhật. Nếu bạn không muốn Eloquent tự động quản lý các cột này, bạn nên xác định thuộc tính

php artisan make:model Flight --migration

29 trên mô hình của mình với giá trị là

php artisan make:model Flight --migration

19

php artisan make:model Flight --migration

2

Nếu bạn cần tùy chỉnh định dạng dấu thời gian của mô hình, hãy đặt thuộc tính

php artisan make:model Flight --migration

31 trên mô hình của bạn. Thuộc tính này xác định cách các thuộc tính ngày được lưu trữ trong cơ sở dữ liệu cũng như định dạng của chúng khi mô hình được tuần tự hóa thành một mảng hoặc JSON

php artisan make:model Flight --migration

3

Nếu bạn cần tùy chỉnh tên của các cột được sử dụng để lưu dấu thời gian, bạn có thể xác định các hằng số

php artisan make:model Flight --migration

32 và

php artisan make:model Flight --migration

33 trên mô hình của mình

php artisan make:model Flight --migration

4

Nếu bạn muốn thực hiện các thao tác mô hình mà không cần sửa đổi dấu thời gian của mô hình, bạn có thể thao tác trên mô hình trong phạm vi bao đóng được cung cấp cho phương thức

php artisan make:model Flight --migration

35

php artisan make:model Flight --migration

5

kết nối cơ sở dữ liệu

Theo mặc định, tất cả các mô hình Eloquent sẽ sử dụng kết nối cơ sở dữ liệu mặc định được định cấu hình cho ứng dụng của bạn. Nếu bạn muốn chỉ định một kết nối khác sẽ được sử dụng khi tương tác với một mô hình cụ thể, bạn nên xác định thuộc tính

php artisan make:model Flight --migration

36 trên mô hình

php artisan make:model Flight --migration

6

Giá trị thuộc tính mặc định

Theo mặc định, một phiên bản mô hình mới được khởi tạo sẽ không chứa bất kỳ giá trị thuộc tính nào. Nếu bạn muốn xác định giá trị mặc định cho một số thuộc tính của mô hình, bạn có thể xác định thuộc tính

php artisan make:model Flight --migration

37 trên mô hình của mình

php artisan make:model Flight --migration

7

Cấu hình Eloquent Strictness

Laravel cung cấp một số phương thức cho phép bạn định cấu hình hành vi và "sự nghiêm ngặt" của Eloquent trong nhiều tình huống khác nhau

Đầu tiên, phương thức

php artisan make:model Flight --migration

38 chấp nhận một đối số boolean tùy chọn cho biết liệu có nên ngăn tải lười biếng hay không. Ví dụ: bạn có thể chỉ muốn tắt tính năng tải chậm trong môi trường phi sản xuất để môi trường sản xuất của bạn sẽ tiếp tục hoạt động bình thường ngay cả khi mối quan hệ tải chậm vô tình xuất hiện trong mã sản xuất. Thông thường, phương thức này nên được gọi trong phương thức

php artisan make:model Flight --migration

39 của ứng dụng của bạn

php artisan make:model Flight --migration

40

php artisan make:model Flight --migration

8

Ngoài ra, bạn có thể hướng dẫn Laravel ném một ngoại lệ khi cố điền một thuộc tính không thể điền bằng cách gọi phương thức

php artisan make:model Flight --migration

41. Điều này có thể giúp ngăn ngừa các lỗi không mong muốn trong quá trình phát triển cục bộ khi cố gắng đặt một thuộc tính chưa được thêm vào mảng

php artisan make:model Flight --migration

42 của mô hình

php artisan make:model Flight --migration

9

Cuối cùng, bạn có thể hướng dẫn Eloquent ném một ngoại lệ nếu bạn cố gắng truy cập một thuộc tính trên một mô hình khi thuộc tính đó không thực sự được lấy từ cơ sở dữ liệu hoặc khi thuộc tính không tồn tại. Ví dụ: điều này có thể xảy ra khi bạn quên thêm thuộc tính vào mệnh đề

php artisan make:model Flight --migration

43 của truy vấn Eloquent

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

0

Kích hoạt "Chế độ nghiêm ngặt" của Eloquent

Để thuận tiện, bạn có thể kích hoạt cả ba phương thức được thảo luận ở trên bằng cách gọi phương thức

php artisan make:model Flight --migration

44

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

1

Truy xuất mô hình

Khi bạn đã tạo một mô hình và bảng cơ sở dữ liệu được liên kết với nó, bạn đã sẵn sàng để bắt đầu truy xuất dữ liệu từ cơ sở dữ liệu của mình. Bạn có thể coi mỗi mô hình Eloquent là một trình tạo truy vấn mạnh mẽ cho phép bạn truy vấn trôi chảy bảng cơ sở dữ liệu được liên kết với mô hình. Phương thức

php artisan make:model Flight --migration

45 của mô hình sẽ lấy tất cả các bản ghi từ bảng cơ sở dữ liệu được liên kết của mô hình

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

2

truy vấn tòa nhà

Phương thức Eloquent

php artisan make:model Flight --migration

45 sẽ trả về tất cả các kết quả trong bảng của mô hình. Tuy nhiên, vì mỗi mô hình Eloquent đóng vai trò là trình tạo truy vấn, bạn có thể thêm các ràng buộc bổ sung cho truy vấn và sau đó gọi phương thức

php artisan make:model Flight --migration

47 để truy xuất kết quả

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

3

Lưu ý
Vì các mô hình Eloquent là trình tạo truy vấn, bạn nên xem lại tất cả các phương thức do trình tạo truy vấn của Laravel cung cấp. Bạn có thể sử dụng bất kỳ phương pháp nào trong số này khi viết truy vấn Eloquent của mình.

Làm mới người mẫu

Nếu bạn đã có một phiên bản của mô hình Eloquent được lấy từ cơ sở dữ liệu, bạn có thể "làm mới" mô hình bằng các phương thức

php artisan make:model Flight --migration

48 và

php artisan make:model Flight --migration

49. Phương thức

php artisan make:model Flight --migration

48 sẽ lấy lại mô hình từ cơ sở dữ liệu. Phiên bản mô hình hiện tại sẽ không bị ảnh hưởng

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

4

Phương pháp

php artisan make:model Flight --migration

49 sẽ hydrat hóa lại mô hình hiện có bằng cách sử dụng dữ liệu mới từ cơ sở dữ liệu. Ngoài ra, tất cả các mối quan hệ đã tải của nó cũng sẽ được làm mới

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

5

bộ sưu tập

Như chúng ta đã thấy, các phương thức Eloquent như

php artisan make:model Flight --migration

45 và

php artisan make:model Flight --migration

47 lấy nhiều bản ghi từ cơ sở dữ liệu. Tuy nhiên, các phương thức này không trả về một mảng PHP đơn giản. Thay vào đó, một thể hiện của

php artisan make:model Flight --migration

54 được trả lại

Lớp Eloquent

php artisan make:model Flight --migration

55 mở rộng lớp cơ sở

php artisan make:model Flight --migration

56 của Laravel, cung cấp nhiều phương thức hữu ích để tương tác với các bộ sưu tập dữ liệu. Ví dụ: phương pháp

php artisan make:model Flight --migration

57 có thể được sử dụng để xóa các mô hình khỏi bộ sưu tập dựa trên kết quả của một lần đóng được gọi

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

6

Ngoài các phương thức được cung cấp bởi class collection cơ sở của Laravel, class Eloquent collection còn cung cấp một số phương thức bổ sung dành riêng cho việc tương tác với các collection của Eloquent models

Vì tất cả các bộ sưu tập của Laravel đều triển khai các giao diện có thể lặp lại của PHP, nên bạn có thể lặp qua các bộ sưu tập như thể chúng là một mảng

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

7

Kết quả Chunking

Ứng dụng của bạn có thể hết bộ nhớ nếu bạn cố tải hàng chục nghìn bản ghi Eloquent thông qua phương thức

php artisan make:model Flight --migration

45 hoặc

php artisan make:model Flight --migration

47. Thay vì sử dụng các phương pháp này, phương pháp

php artisan make:model Flight --migration

60 có thể được sử dụng để xử lý số lượng lớn các mô hình hiệu quả hơn

Phương thức

php artisan make:model Flight --migration

60 sẽ truy xuất một tập hợp con của các mô hình Eloquent, chuyển chúng tới một bao đóng để xử lý. Vì mỗi lần chỉ truy xuất một đoạn mô hình Eloquent hiện tại, nên phương thức

php artisan make:model Flight --migration

60 sẽ giúp giảm đáng kể mức sử dụng bộ nhớ khi làm việc với một số lượng lớn mô hình

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

8

Đối số đầu tiên được truyền cho phương thức

php artisan make:model Flight --migration

60 là số bản ghi bạn muốn nhận trên mỗi "khối". Việc đóng được thông qua khi đối số thứ hai sẽ được gọi cho mỗi đoạn được truy xuất từ ​​​​cơ sở dữ liệu. Một truy vấn cơ sở dữ liệu sẽ được thực hiện để truy xuất từng đoạn bản ghi được chuyển đến phần đóng

Nếu bạn đang lọc kết quả của phương pháp

php artisan make:model Flight --migration

60 dựa trên một cột mà bạn cũng sẽ cập nhật trong khi lặp lại kết quả, bạn nên sử dụng phương pháp

php artisan make:model Flight --migration

65. Sử dụng phương pháp

php artisan make:model Flight --migration

60 trong các tình huống này có thể dẫn đến kết quả không mong muốn và không nhất quán. Bên trong, phương thức

php artisan make:model Flight --migration

65 sẽ luôn truy xuất các mô hình có cột

php artisan make:model Flight --migration

16 lớn hơn mô hình cuối cùng trong đoạn trước

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

9

Chunking Sử dụng Lazy Collections

Phương thức

php artisan make:model Flight --migration

69 hoạt động tương tự như phương thức

php artisan make:model Flight --migration

60 theo nghĩa là, đằng sau hậu trường, nó thực thi truy vấn theo khối. Tuy nhiên, thay vì chuyển từng đoạn trực tiếp vào một hàm gọi lại như hiện tại, phương thức

php artisan make:model Flight --migration

69 trả về một

php artisan make:model Flight --migration

72 phẳng của các mô hình Eloquent, cho phép bạn tương tác với các kết quả dưới dạng một luồng duy nhất

php artisan make:model Flight --migration

00

Nếu bạn đang lọc kết quả của phương pháp

php artisan make:model Flight --migration

69 dựa trên một cột mà bạn cũng sẽ cập nhật trong khi lặp lại kết quả, bạn nên sử dụng phương pháp

php artisan make:model Flight --migration

74. Bên trong, phương thức

php artisan make:model Flight --migration

74 sẽ luôn truy xuất các mô hình có cột

php artisan make:model Flight --migration

16 lớn hơn mô hình cuối cùng trong đoạn trước

php artisan make:model Flight --migration

01

Bạn có thể lọc kết quả dựa trên thứ tự giảm dần của

php artisan make:model Flight --migration

16 bằng phương pháp

php artisan make:model Flight --migration

78

con trỏ

Tương tự như phương pháp

php artisan make:model Flight --migration

69, phương pháp

php artisan make:model Flight --migration

80 có thể được sử dụng để giảm đáng kể mức tiêu thụ bộ nhớ của ứng dụng khi lặp qua hàng chục nghìn bản ghi mô hình Eloquent

Phương thức

php artisan make:model Flight --migration

80 sẽ chỉ thực hiện một truy vấn cơ sở dữ liệu duy nhất; . Do đó, chỉ có một mô hình Eloquent được lưu trong bộ nhớ tại bất kỳ thời điểm nào trong khi lặp qua con trỏ

Cảnh báo
Vì phương thức

php artisan make:model Flight --migration

80 chỉ giữ một mô hình Eloquent duy nhất trong bộ nhớ tại một thời điểm, nên nó không thể háo hức tải các mối quan hệ. Nếu bạn cần háo hức tải các mối quan hệ, hãy cân nhắc sử dụng phương thức

php artisan make:model Flight --migration

69 để thay thế.

Bên trong, phương thức

php artisan make:model Flight --migration

80 sử dụng trình tạo PHP để thực hiện chức năng này

php artisan make:model Flight --migration

02

php artisan make:model Flight --migration

80 trả về một phiên bản

php artisan make:model Flight --migration

86. Các bộ sưu tập lười biếng cho phép bạn sử dụng nhiều phương thức sưu tập có sẵn trên các bộ sưu tập điển hình của Laravel trong khi chỉ tải một mô hình duy nhất vào bộ nhớ tại một thời điểm

php artisan make:model Flight --migration

03

Mặc dù phương thức

php artisan make:model Flight --migration

80 sử dụng ít bộ nhớ hơn nhiều so với truy vấn thông thường (bằng cách chỉ giữ một mô hình Eloquent duy nhất trong bộ nhớ tại một thời điểm), nhưng cuối cùng nó vẫn sẽ hết bộ nhớ. Điều này là do trình điều khiển PDO của PHP lưu trữ nội bộ tất cả các kết quả truy vấn thô trong bộ đệm của nó. Nếu bạn đang xử lý một số lượng rất lớn các bản ghi Eloquent, hãy cân nhắc sử dụng phương pháp

php artisan make:model Flight --migration

69 để thay thế

Truy vấn con nâng cao

Lựa chọn truy vấn con

Eloquent cũng cung cấp hỗ trợ truy vấn con nâng cao, cho phép bạn lấy thông tin từ các bảng có liên quan trong một truy vấn duy nhất. Ví dụ: hãy tưởng tượng rằng chúng ta có một bảng chuyến bay

php artisan make:model Flight --migration

89 và một bảng

php artisan make:model Flight --migration

12 đến các điểm đến. Bảng

php artisan make:model Flight --migration

12 chứa cột

php artisan make:model Flight --migration

92 cho biết thời điểm chuyến bay đến đích

Sử dụng chức năng truy vấn phụ có sẵn cho các phương pháp

php artisan make:model Flight --migration

43 và

php artisan make:model Flight --migration

94 của trình tạo truy vấn, chúng tôi có thể chọn tất cả

php artisan make:model Flight --migration

89 và tên của chuyến bay đã đến điểm đến đó gần đây nhất bằng một truy vấn duy nhất

php artisan make:model Flight --migration

04

Thứ tự truy vấn con

Ngoài ra, hàm

php artisan make:model Flight --migration

96 của trình tạo truy vấn hỗ trợ các truy vấn con. Tiếp tục sử dụng ví dụ về chuyến bay của chúng tôi, chúng tôi có thể sử dụng chức năng này để sắp xếp tất cả các điểm đến dựa trên thời điểm chuyến bay cuối cùng đến điểm đến đó. Một lần nữa, điều này có thể được thực hiện trong khi thực hiện một truy vấn cơ sở dữ liệu

php artisan make:model Flight --migration

05

Truy xuất các mô hình đơn lẻ / tổng hợp

Ngoài việc truy xuất tất cả các bản ghi phù hợp với một truy vấn nhất định, bạn cũng có thể truy xuất các bản ghi đơn lẻ bằng các phương pháp

php artisan make:model Flight --migration

97,

php artisan make:model Flight --migration

98 hoặc

php artisan make:model Flight --migration

99. Thay vì trả về một tập hợp các mô hình, các phương thức này trả về một phiên bản mô hình duy nhất

php artisan make:model Flight --migration

06

Đôi khi bạn có thể muốn thực hiện một số hành động khác nếu không tìm thấy kết quả. Các phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

00 và

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

01 sẽ trả về một phiên bản mô hình duy nhất hoặc nếu không tìm thấy kết quả nào, hãy thực hiện lệnh đóng đã cho. Giá trị trả về của bao đóng sẽ được coi là kết quả của phương thức

php artisan make:model Flight --migration

07

Không tìm thấy ngoại lệ

Đôi khi bạn có thể muốn ném một ngoại lệ nếu không tìm thấy mô hình. Điều này đặc biệt hữu ích trong các tuyến đường hoặc bộ điều khiển. Các phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

02 và

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

03 sẽ lấy kết quả đầu tiên của truy vấn;

php artisan make:model Flight --migration

08

Nếu không bắt được

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

05, phản hồi HTTP 404 sẽ tự động được gửi lại cho máy khách

php artisan make:model Flight --migration

09

Truy xuất hoặc tạo mô hình

Phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

06 sẽ cố gắng định vị một bản ghi cơ sở dữ liệu bằng cách sử dụng các cặp cột/giá trị đã cho. Nếu không thể tìm thấy mô hình trong cơ sở dữ liệu, một bản ghi sẽ được chèn với các thuộc tính do hợp nhất đối số mảng thứ nhất với đối số mảng thứ hai tùy chọn

Phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

07, giống như

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

06, sẽ cố gắng xác định vị trí một bản ghi trong cơ sở dữ liệu khớp với các thuộc tính đã cho. Tuy nhiên, nếu không tìm thấy mô hình, một phiên bản mô hình mới sẽ được trả về. Lưu ý rằng mô hình được trả về bởi

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

07 vẫn chưa được lưu vào cơ sở dữ liệu. Bạn sẽ cần gọi thủ công phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

10 để duy trì nó

php artisan make:model Flight --migration

30

Truy xuất tổng hợp

Khi tương tác với các mô hình Eloquent, bạn cũng có thể sử dụng các phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

11,

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

12,

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

13 và các phương thức tổng hợp khác do trình tạo truy vấn Laravel cung cấp. Như bạn có thể mong đợi, các phương thức này trả về một giá trị vô hướng thay vì một thể hiện của Eloquent model

php artisan make:model Flight --migration

31

Chèn và cập nhật mô hình

phụ trang

Tất nhiên, khi sử dụng Eloquent, chúng ta không chỉ cần lấy các model từ cơ sở dữ liệu. Chúng ta cũng cần chèn các bản ghi mới. Rất may, Eloquent làm cho nó đơn giản. Để chèn một bản ghi mới vào cơ sở dữ liệu, bạn nên khởi tạo một phiên bản mô hình mới và đặt các thuộc tính trên mô hình. Sau đó, gọi phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

10 trên phiên bản mô hình

php artisan make:model Flight --migration

32

Trong ví dụ này, chúng tôi chỉ định trường

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

15 từ yêu cầu HTTP đến cho thuộc tính

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

15 của phiên bản mô hình

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

17. Khi chúng ta gọi phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

10, một bản ghi sẽ được chèn vào cơ sở dữ liệu. Dấu thời gian

php artisan make:model Flight --migration

27 và

php artisan make:model Flight --migration

28 của mô hình sẽ tự động được đặt khi phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

10 được gọi, vì vậy không cần phải đặt chúng theo cách thủ công

Ngoài ra, bạn có thể sử dụng phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

22 để "lưu" một mô hình mới bằng một câu lệnh PHP. Phiên bản mô hình được chèn sẽ được trả về cho bạn bằng phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

22

php artisan make:model Flight --migration

33

Tuy nhiên, trước khi sử dụng phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

22, bạn cần chỉ định thuộc tính

php artisan make:model Flight --migration

42 hoặc

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

26 trên lớp mô hình của mình. Các thuộc tính này là bắt buộc vì tất cả các mô hình Eloquent đều được bảo vệ khỏi các lỗ hổng gán hàng loạt theo mặc định. Để tìm hiểu thêm về phép gán hàng loạt, vui lòng tham khảo tài liệu về phép gán hàng loạt

cập nhật

Phương pháp

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

10 cũng có thể được sử dụng để cập nhật các mô hình đã tồn tại trong cơ sở dữ liệu. Để cập nhật một mô hình, bạn nên truy xuất mô hình đó và đặt bất kỳ thuộc tính nào bạn muốn cập nhật. Sau đó, bạn nên gọi phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

10 của mô hình. Một lần nữa, dấu thời gian

php artisan make:model Flight --migration

28 sẽ tự động được cập nhật, do đó không cần phải đặt giá trị của nó theo cách thủ công

php artisan make:model Flight --migration

34

Cập nhật hàng loạt

Cập nhật cũng có thể được thực hiện đối với các mô hình phù hợp với một truy vấn nhất định. Trong ví dụ này, tất cả các chuyến bay là

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

30 và có

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

31 của

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

32 sẽ được đánh dấu là bị hoãn

php artisan make:model Flight --migration

35

Phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

33 yêu cầu một mảng các cặp cột và giá trị đại diện cho các cột cần được cập nhật. Phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

33 trả về số hàng bị ảnh hưởng

Cảnh báo
Khi phát hành bản cập nhật hàng loạt qua Eloquent, các sự kiện mô hình

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

35,

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

36,

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

37 và

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

38 sẽ không được kích hoạt cho các mô hình đã cập nhật. Điều này là do các mô hình không bao giờ thực sự được truy xuất khi phát hành bản cập nhật hàng loạt.

Kiểm tra thay đổi thuộc tính

Eloquent cung cấp các phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

39,

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

40 và

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

41 để kiểm tra trạng thái bên trong mô hình của bạn và xác định các thuộc tính của mô hình đã thay đổi như thế nào so với khi mô hình được truy xuất ban đầu

Phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

39 xác định xem có bất kỳ thuộc tính nào của mô hình đã bị thay đổi kể từ khi mô hình được truy xuất hay không. Bạn có thể chuyển một tên thuộc tính cụ thể hoặc một mảng thuộc tính cho phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

39 để xác định xem có thuộc tính nào "bẩn" không. Phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

40 sẽ xác định xem một thuộc tính có không thay đổi kể từ khi mô hình được truy xuất hay không. Phương thức này cũng chấp nhận một đối số thuộc tính tùy chọn

php artisan make:model Flight --migration

36

Phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

41 xác định xem có bất kỳ thuộc tính nào bị thay đổi khi mô hình được lưu lần cuối trong chu kỳ yêu cầu hiện tại hay không. Nếu cần, bạn có thể chuyển tên thuộc tính để xem liệu một thuộc tính cụ thể có bị thay đổi hay không

php artisan make:model Flight --migration

37

Phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

46 trả về một mảng chứa các thuộc tính ban đầu của mô hình bất kể có bất kỳ thay đổi nào đối với mô hình kể từ khi nó được truy xuất. Nếu cần, bạn có thể chuyển một tên thuộc tính cụ thể để lấy giá trị ban đầu của một thuộc tính cụ thể

php artisan make:model Flight --migration

38

chuyển nhượng hàng loạt

Bạn có thể sử dụng phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

22 để "lưu" một mô hình mới bằng một câu lệnh PHP. Phiên bản mô hình được chèn sẽ được trả về cho bạn theo phương thức

php artisan make:model Flight --migration

33

Tuy nhiên, trước khi sử dụng phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

22, bạn cần chỉ định thuộc tính

php artisan make:model Flight --migration

42 hoặc

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

26 trên lớp mô hình của mình. Các thuộc tính này là bắt buộc vì tất cả các mô hình Eloquent đều được bảo vệ khỏi các lỗ hổng gán hàng loạt theo mặc định

Lỗ hổng gán hàng loạt xảy ra khi người dùng chuyển một trường yêu cầu HTTP không mong muốn và trường đó thay đổi một cột trong cơ sở dữ liệu của bạn mà bạn không mong đợi. Ví dụ: người dùng ác ý có thể gửi tham số

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

51 thông qua yêu cầu HTTP, sau đó yêu cầu này được chuyển đến phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

22 của mô hình của bạn, cho phép người dùng tự báo cáo với quản trị viên

Vì vậy, để bắt đầu, bạn nên xác định thuộc tính mô hình nào bạn muốn gán hàng loạt. Bạn có thể làm điều này bằng cách sử dụng thuộc tính

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

53 trên mô hình. Ví dụ: hãy đặt thuộc tính

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

15 của mô hình

php artisan make:model Flight --migration

10 có thể gán khối lượng

php artisan make:model Flight --migration

00

Khi bạn đã chỉ định thuộc tính nào có thể gán hàng loạt, bạn có thể sử dụng phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

22 để chèn bản ghi mới vào cơ sở dữ liệu. Phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

22 trả về thể hiện mô hình mới được tạo

php artisan make:model Flight --migration

01

Nếu bạn đã có một phiên bản mô hình, bạn có thể sử dụng phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

58 để điền vào đó một mảng các thuộc tính

php artisan make:model Flight --migration

02

Chuyển nhượng hàng loạt & cột JSON

Khi gán các cột JSON, khóa có thể gán hàng loạt của mỗi cột phải được chỉ định trong mảng

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

53 của mô hình của bạn. Để bảo mật, Laravel không hỗ trợ cập nhật các thuộc tính JSON lồng nhau khi sử dụng thuộc tính

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

26

php artisan make:model Flight --migration

03

Cho phép chuyển nhượng hàng loạt

Nếu bạn muốn làm cho tất cả các thuộc tính của mình có thể gán hàng loạt, bạn có thể xác định thuộc tính

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

61 của mô hình của mình dưới dạng một mảng trống. Nếu bạn chọn không bảo vệ mô hình của mình, bạn nên đặc biệt cẩn thận để luôn tạo thủ công các mảng được truyền cho các phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

58,

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

22 và

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

33 của Eloquent

php artisan make:model Flight --migration

04

Ngoại lệ chuyển nhượng hàng loạt

Theo mặc định, các thuộc tính không được bao gồm trong mảng

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

53 sẽ bị loại bỏ một cách âm thầm khi thực hiện các thao tác gán hàng loạt. Trong sản xuất, đây là hành vi dự kiến;

Nếu muốn, bạn có thể hướng dẫn Laravel ném một ngoại lệ khi cố điền một thuộc tính không thể điền bằng cách gọi phương thức

php artisan make:model Flight --migration

41. Thông thường, phương thức này nên được gọi trong phương thức

php artisan make:model Flight --migration

39 của một trong những nhà cung cấp dịch vụ ứng dụng của bạn

php artisan make:model Flight --migration

05

Upserts

Đôi khi, bạn có thể cần cập nhật mô hình hiện có hoặc tạo mô hình mới nếu không có mô hình phù hợp. Giống như phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

06, phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

69 vẫn duy trì mô hình, vì vậy không cần phải gọi phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

10 theo cách thủ công

Trong ví dụ dưới đây, nếu một chuyến bay tồn tại với vị trí

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

71 của

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

72 và vị trí

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

31 của

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

32, các cột

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

75 và

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

76 của chuyến bay đó sẽ được cập nhật. Nếu không có chuyến bay nào như vậy tồn tại, một chuyến bay mới sẽ được tạo có các thuộc tính do hợp nhất mảng đối số thứ nhất với mảng đối số thứ hai

php artisan make:model Flight --migration

06

Nếu bạn muốn thực hiện nhiều "uperts" trong một truy vấn, thì bạn nên sử dụng phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

77 để thay thế. Đối số đầu tiên của phương thức bao gồm các giá trị để chèn hoặc cập nhật, trong khi đối số thứ hai liệt kê (các) cột xác định duy nhất các bản ghi trong bảng được liên kết. Đối số thứ ba và cũng là đối số cuối cùng của phương thức là một mảng các cột sẽ được cập nhật nếu một bản ghi phù hợp đã tồn tại trong cơ sở dữ liệu. Phương pháp

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

77 sẽ tự động đặt dấu thời gian

php artisan make:model Flight --migration

27 và

php artisan make:model Flight --migration

28 nếu dấu thời gian được bật trên mô hình

php artisan make:model Flight --migration

07

Cảnh báo
Tất cả các cơ sở dữ liệu ngoại trừ SQL Server đều yêu cầu các cột trong đối số thứ hai của phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

77 phải có chỉ mục "chính" hoặc "duy nhất". Ngoài ra, trình điều khiển cơ sở dữ liệu MySQL bỏ qua đối số thứ hai của phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

77 và luôn sử dụng các chỉ mục "chính" và "duy nhất" của bảng để phát hiện các bản ghi hiện có.

Xóa mô hình

Để xóa một mô hình, bạn có thể gọi phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

83 trên phiên bản mô hình

Bạn có thể gọi phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

84 để xóa tất cả các bản ghi cơ sở dữ liệu liên quan của mô hình. Thao tác

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

84 cũng sẽ đặt lại mọi ID tăng tự động trên bảng được liên kết của mô hình

Xóa một mô hình hiện có bằng khóa chính của nó

Trong ví dụ trên, chúng tôi đang truy xuất mô hình từ cơ sở dữ liệu trước khi gọi phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

83. Tuy nhiên, nếu bạn biết khóa chính của mô hình, bạn có thể xóa mô hình mà không truy xuất nó một cách rõ ràng bằng cách gọi phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

87. Ngoài việc chấp nhận một khóa chính duy nhất, phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

87 sẽ chấp nhận nhiều khóa chính, một mảng khóa chính hoặc tập hợp các khóa chính

php artisan make:model Flight --migration

08

Cảnh báo
Phương pháp

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

87 tải từng mô hình riêng lẻ và gọi phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

83 để các sự kiện

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

91 và

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

92 được gửi đúng cách cho từng mô hình.

Xóa mô hình bằng truy vấn

Tất nhiên, bạn có thể tạo truy vấn Eloquent để xóa tất cả các mô hình phù hợp với tiêu chí truy vấn của bạn. Trong ví dụ này, chúng tôi sẽ xóa tất cả các chuyến bay được đánh dấu là không hoạt động. Giống như cập nhật hàng loạt, xóa hàng loạt sẽ không gửi các sự kiện mô hình cho các mô hình bị xóa

php artisan make:model Flight --migration

09

Cảnh báo
Khi thực hiện lệnh xóa hàng loạt qua Eloquent, các sự kiện mô hình

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

91 và

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

92 sẽ không được gửi cho các mô hình đã xóa. Điều này là do các mô hình không bao giờ thực sự được truy xuất khi thực hiện câu lệnh xóa.

Xóa mềm

Ngoài việc thực sự xóa các bản ghi khỏi cơ sở dữ liệu của bạn, Eloquent còn có thể "xóa mềm" các mô hình. Khi các mô hình bị xóa mềm, chúng không thực sự bị xóa khỏi cơ sở dữ liệu của bạn. Thay vào đó, một thuộc tính

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

95 được đặt trên mô hình cho biết ngày và giờ mà mô hình bị "xóa". Để kích hoạt xóa mềm cho một mô hình, hãy thêm đặc điểm

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

96 vào mô hình

php artisan make:model Flight --migration

30

Lưu ý
Đặc điểm

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

97 sẽ tự động chuyển thuộc tính

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

95 thành phiên bản

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

99 /

php artisan make:model Flight --migration

000 cho bạn.

Bạn cũng nên thêm cột

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

95 vào bảng cơ sở dữ liệu của mình. Trình tạo lược đồ Laravel chứa một phương thức trợ giúp để tạo cột này

php artisan make:model Flight --migration

31

Bây giờ, khi bạn gọi phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

83 trên model, cột

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

95 sẽ được đặt thành ngày và giờ hiện tại. Tuy nhiên, bản ghi cơ sở dữ liệu của mô hình sẽ được để lại trong bảng. Khi truy vấn một mô hình sử dụng xóa mềm, các mô hình đã xóa mềm sẽ tự động bị loại khỏi tất cả các kết quả truy vấn

Để xác định xem một phiên bản mô hình nhất định đã bị xóa mềm hay chưa, bạn có thể sử dụng phương pháp

php artisan make:model Flight --migration

004

Khôi phục các mô hình đã xóa mềm

Đôi khi bạn có thể muốn "hủy xóa" một mô hình đã xóa mềm. Để khôi phục một mô hình đã xóa mềm, bạn có thể gọi phương thức

php artisan make:model Flight --migration

005 trên một phiên bản mô hình. Phương pháp

php artisan make:model Flight --migration

005 sẽ đặt cột

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

95 của mô hình thành

php artisan make:model Flight --migration

008

Bạn cũng có thể sử dụng phương pháp

php artisan make:model Flight --migration

005 trong truy vấn để khôi phục nhiều kiểu máy. Một lần nữa, giống như các hoạt động "khối lượng" khác, thao tác này sẽ không gửi bất kỳ sự kiện mô hình nào cho các mô hình được khôi phục

Phương pháp

php artisan make:model Flight --migration

005 cũng có thể được sử dụng khi xây dựng các truy vấn mối quan hệ

php artisan make:model Flight --migration

32

Xóa vĩnh viễn các mô hình

Đôi khi bạn có thể cần thực sự xóa một mô hình khỏi cơ sở dữ liệu của mình. Bạn có thể sử dụng phương pháp

php artisan make:model Flight --migration

011 để xóa vĩnh viễn mô hình đã xóa mềm khỏi bảng cơ sở dữ liệu

Bạn cũng có thể sử dụng phương thức

php artisan make:model Flight --migration

011 khi xây dựng các truy vấn quan hệ Eloquent

php artisan make:model Flight --migration

33

Truy vấn các mô hình đã xóa mềm

Bao gồm cả các mô hình đã xóa mềm

Như đã lưu ý ở trên, các mô hình đã xóa mềm sẽ tự động bị loại khỏi kết quả truy vấn. Tuy nhiên, bạn có thể buộc các mô hình đã xóa mềm được đưa vào kết quả của truy vấn bằng cách gọi phương thức

php artisan make:model Flight --migration

013 trên truy vấn

php artisan make:model Flight --migration

34

Phương thức

php artisan make:model Flight --migration

013 cũng có thể được gọi khi xây dựng truy vấn mối quan hệ

php artisan make:model Flight --migration

35

Chỉ truy xuất các mô hình đã xóa mềm

Phương pháp

php artisan make:model Flight --migration

015 sẽ chỉ truy xuất các mô hình đã xóa mềm

php artisan make:model Flight --migration

36

Mô hình cắt tỉa

Đôi khi bạn có thể muốn xóa định kỳ các mô hình không còn cần thiết. Để thực hiện điều này, bạn có thể thêm đặc điểm

php artisan make:model Flight --migration

016 hoặc

php artisan make:model Flight --migration

017 vào các mô hình mà bạn muốn cắt tỉa định kỳ. Sau khi thêm một trong các đặc điểm vào mô hình, hãy triển khai phương thức

php artisan make:model Flight --migration

018 trả về trình tạo truy vấn Eloquent để giải quyết các mô hình không còn cần thiết

php artisan make:model Flight --migration

37

Khi đánh dấu các mô hình là

php artisan make:model Flight --migration

019, bạn cũng có thể xác định một phương pháp

php artisan make:model Flight --migration

020 trên mô hình. Phương thức này sẽ được gọi trước khi mô hình bị xóa. Phương pháp này có thể hữu ích để xóa bất kỳ tài nguyên bổ sung nào được liên kết với mô hình, chẳng hạn như các tệp được lưu trữ, trước khi mô hình bị xóa vĩnh viễn khỏi cơ sở dữ liệu

php artisan make:model Flight --migration

38

Sau khi định cấu hình mô hình có thể cắt được của bạn, bạn nên lên lịch cho lệnh Artisan

php artisan make:model Flight --migration

021 trong lớp

php artisan make:model Flight --migration

022 của ứng dụng của bạn. Bạn có thể tự do chọn khoảng thời gian thích hợp để chạy lệnh này

php artisan make:model Flight --migration

39

Đằng sau hậu trường, lệnh

php artisan make:model Flight --migration

021 sẽ tự động phát hiện các mô hình "Có thể cắt được" trong thư mục

php artisan make:model Flight --migration

09 của ứng dụng của bạn. Nếu các mô hình của bạn ở một vị trí khác, bạn có thể sử dụng tùy chọn

php artisan make:model Flight --migration

025 để chỉ định tên lớp mô hình

php artisan make:model Flight --migration

60

Nếu bạn muốn loại trừ một số mô hình khỏi bị cắt tỉa trong khi cắt tỉa tất cả các mô hình được phát hiện khác, bạn có thể sử dụng tùy chọn

php artisan make:model Flight --migration

026

php artisan make:model Flight --migration

61

Bạn có thể kiểm tra truy vấn

php artisan make:model Flight --migration

018 của mình bằng cách thực hiện lệnh

php artisan make:model Flight --migration

021 với tùy chọn

php artisan make:model Flight --migration

029. Khi giả vờ, lệnh

php artisan make:model Flight --migration

021 sẽ chỉ báo cáo có bao nhiêu bản ghi sẽ bị cắt bớt nếu lệnh thực sự chạy

php artisan make:model Flight --migration

62

Cảnh báo
Các mô hình xóa mềm sẽ bị xóa vĩnh viễn (

php artisan make:model Flight --migration

011) nếu chúng phù hợp với truy vấn có thể cắt được.

cắt tỉa hàng loạt

Khi các mô hình được đánh dấu bằng đặc điểm

php artisan make:model Flight --migration

017, các mô hình sẽ bị xóa khỏi cơ sở dữ liệu bằng truy vấn xóa hàng loạt. Do đó, phương thức

php artisan make:model Flight --migration

020 sẽ không được gọi, cũng như các sự kiện mô hình

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

91 và

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

92 sẽ không được gửi đi. Điều này là do các mô hình không bao giờ thực sự được truy xuất trước khi xóa, do đó làm cho quá trình cắt tỉa hiệu quả hơn nhiều

php artisan make:model Flight --migration

63

Sao chép mô hình

Bạn có thể tạo một bản sao chưa lưu của một phiên bản mô hình hiện có bằng cách sử dụng phương pháp

php artisan make:model Flight --migration

036. Phương pháp này đặc biệt hữu ích khi bạn có các phiên bản mô hình chia sẻ nhiều thuộc tính giống nhau

php artisan make:model Flight --migration

64

Để loại trừ một hoặc nhiều thuộc tính không được sao chép sang mô hình mới, bạn có thể chuyển một mảng cho phương thức

php artisan make:model Flight --migration

036

php artisan make:model Flight --migration

65

Phạm vi truy vấn

Phạm vi toàn cầu

Phạm vi toàn cầu cho phép bạn thêm các ràng buộc vào tất cả các truy vấn cho một mô hình nhất định. Chức năng xóa mềm của Laravel sử dụng phạm vi toàn cầu để chỉ truy xuất các mô hình "không bị xóa" khỏi cơ sở dữ liệu. Viết phạm vi toàn cầu của riêng bạn có thể cung cấp một cách thuận tiện, dễ dàng để đảm bảo mọi truy vấn cho một mô hình nhất định đều nhận được các ràng buộc nhất định

Viết phạm vi toàn cầu

Viết một phạm vi toàn cầu là đơn giản. Đầu tiên, xác định một lớp triển khai giao diện

php artisan make:model Flight --migration

038. Laravel không có một vị trí thông thường nơi bạn nên đặt các lớp phạm vi, vì vậy bạn có thể đặt lớp này trong bất kỳ thư mục nào bạn muốn

Giao diện

php artisan make:model Flight --migration

039 yêu cầu bạn triển khai một phương thức.

php artisan make:model Flight --migration

040. Phương thức

php artisan make:model Flight --migration

040 có thể thêm các ràng buộc

php artisan make:model Flight --migration

042 hoặc các loại mệnh đề khác vào truy vấn khi cần

php artisan make:model Flight --migration

66

Lưu ý
Nếu phạm vi toàn cục của bạn đang thêm các cột vào mệnh đề select của truy vấn, bạn nên sử dụng phương thức

php artisan make:model Flight --migration

94 thay vì

php artisan make:model Flight --migration

43. Điều này sẽ ngăn việc thay thế không chủ ý mệnh đề select hiện có của truy vấn.

Áp dụng phạm vi toàn cầu

Để gán phạm vi toàn cầu cho một mô hình, bạn nên ghi đè phương thức

php artisan make:model Flight --migration

045 của mô hình và gọi phương thức

php artisan make:model Flight --migration

046 của mô hình. Phương thức

php artisan make:model Flight --migration

046 chấp nhận một thể hiện trong phạm vi của bạn làm đối số duy nhất của nó

php artisan make:model Flight --migration

67

Sau khi thêm phạm vi trong ví dụ trên vào mô hình

php artisan make:model Flight --migration

048, lệnh gọi phương thức

php artisan make:model Flight --migration

049 sẽ thực thi truy vấn SQL sau

php artisan make:model Flight --migration

68

Phạm vi toàn cầu ẩn danh

Eloquent cũng cho phép bạn xác định các phạm vi toàn cầu bằng cách sử dụng các bao đóng, điều này đặc biệt hữu ích cho các phạm vi đơn giản không đảm bảo một lớp riêng biệt của riêng chúng. Khi xác định phạm vi toàn cầu bằng cách sử dụng bao đóng, bạn nên cung cấp tên phạm vi do chính bạn chọn làm đối số đầu tiên cho phương thức

php artisan make:model Flight --migration

046

php artisan make:model Flight --migration

69

Loại bỏ phạm vi toàn cầu

Nếu bạn muốn xóa phạm vi toàn cầu cho một truy vấn nhất định, bạn có thể sử dụng phương pháp

php artisan make:model Flight --migration

051. Phương thức này chấp nhận tên lớp của phạm vi toàn cầu làm đối số duy nhất của nó

php artisan make:model Flight --migration

90

Hoặc, nếu bạn đã xác định phạm vi toàn cầu bằng bao đóng, bạn nên chuyển tên chuỗi mà bạn đã gán cho phạm vi toàn cầu

php artisan make:model Flight --migration

91

Nếu bạn muốn xóa một số hoặc thậm chí tất cả phạm vi toàn cầu của truy vấn, bạn có thể sử dụng phương pháp

php artisan make:model Flight --migration

052

php artisan make:model Flight --migration

92

Phạm vi cục bộ

Phạm vi cục bộ cho phép bạn xác định các bộ ràng buộc truy vấn phổ biến mà bạn có thể dễ dàng sử dụng lại trong ứng dụng của mình. Ví dụ: bạn có thể cần thường xuyên truy xuất tất cả người dùng được coi là "phổ biến". Để xác định phạm vi, thêm tiền tố vào một phương thức mô hình Eloquent bằng

php artisan make:model Flight --migration

053

Phạm vi phải luôn trả về cùng một phiên bản trình tạo truy vấn hoặc

php artisan make:model Flight --migration

054

php artisan make:model Flight --migration

93

Sử dụng phạm vi cục bộ

Khi phạm vi đã được xác định, bạn có thể gọi các phương thức phạm vi khi truy vấn mô hình. Tuy nhiên, bạn không nên bao gồm tiền tố

php artisan make:model Flight --migration

053 khi gọi phương thức. Bạn thậm chí có thể xâu chuỗi các cuộc gọi đến các phạm vi khác nhau

php artisan make:model Flight --migration

94

Kết hợp nhiều phạm vi mô hình Eloquent thông qua toán tử truy vấn

php artisan make:model Flight --migration

056 có thể yêu cầu sử dụng các bao đóng để đạt được nhóm logic chính xác

php artisan make:model Flight --migration

95

Tuy nhiên, vì điều này có thể rườm rà, nên Laravel cung cấp phương thức

php artisan make:model Flight --migration

057 "bậc cao hơn" cho phép bạn xâu chuỗi các phạm vi lại với nhau một cách trôi chảy mà không cần sử dụng bao đóng

php artisan make:model Flight --migration

96

Phạm vi động

Đôi khi bạn có thể muốn xác định một phạm vi chấp nhận các tham số. Để bắt đầu, chỉ cần thêm các tham số bổ sung vào chữ ký của phương thức phạm vi của bạn. Tham số phạm vi phải được xác định sau tham số

php artisan make:model Flight --migration

058

php artisan make:model Flight --migration

97

Khi các đối số dự kiến ​​đã được thêm vào chữ ký của phương thức phạm vi của bạn, bạn có thể chuyển các đối số khi gọi phạm vi

php artisan make:model Flight --migration

98

So sánh các mô hình

Đôi khi bạn có thể cần xác định xem hai mô hình có "giống nhau" hay không. Các phương pháp

php artisan make:model Flight --migration

059 và

php artisan make:model Flight --migration

060 có thể được sử dụng để nhanh chóng xác minh hai mô hình có cùng khóa chính, bảng và kết nối cơ sở dữ liệu hay không

php artisan make:model Flight --migration

99

Các phương pháp

php artisan make:model Flight --migration

059 và

php artisan make:model Flight --migration

060 cũng khả dụng khi sử dụng các mối quan hệ ________ 1063, ________ 1064, ______ 1065 và ________ 1066. Phương pháp này đặc biệt hữu ích khi bạn muốn so sánh một mô hình có liên quan mà không đưa ra truy vấn để truy xuất mô hình đó

php artisan make:model Flight --migration

10

Sự kiện

Lưu ý
Bạn muốn phát trực tiếp các sự kiện Eloquent tới ứng dụng phía máy khách của mình? .

Các mô hình Eloquent gửi một số sự kiện, cho phép bạn kết nối với những khoảnh khắc sau trong vòng đời của một mô hình. ________ 1067, ________ 1068, ______ 1069,

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

37,

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

38,

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

35,

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

36,

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

91,

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

92,

php artisan make:model Flight --migration

004,

php artisan make:model Flight --migration

077,

php artisan make:model Flight --migration

078,

php artisan make:model Flight --migration

079 và

php artisan make:model Flight --migration

080

Sự kiện

php artisan make:model Flight --migration

067 sẽ gửi đi khi một mô hình hiện có được lấy từ cơ sở dữ liệu. Khi một mô hình mới được lưu lần đầu tiên, các sự kiện

php artisan make:model Flight --migration

068 và

php artisan make:model Flight --migration

069 sẽ gửi đi. Các sự kiện ________ 837 / ________ 838 sẽ gửi đi khi một mô hình hiện có được sửa đổi và phương thức

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

10 được gọi. Các sự kiện

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

35 /

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

36 sẽ gửi đi khi một mô hình được tạo hoặc cập nhật - ngay cả khi các thuộc tính của mô hình không bị thay đổi. Tên sự kiện kết thúc bằng

php artisan make:model Flight --migration

089 được gửi đi trước khi bất kỳ thay đổi nào đối với mô hình được duy trì, trong khi các sự kiện kết thúc bằng

php artisan make:model Flight --migration

090 được gửi đi sau khi các thay đổi đối với mô hình được duy trì

Để bắt đầu lắng nghe các sự kiện mô hình, hãy xác định thuộc tính

php artisan make:model Flight --migration

091 trên mô hình Eloquent của bạn. Thuộc tính này ánh xạ các điểm khác nhau trong vòng đời của mô hình Eloquent tới các lớp sự kiện của riêng bạn. Mỗi lớp sự kiện mô hình sẽ nhận được một phiên bản của mô hình bị ảnh hưởng thông qua hàm tạo của nó

php artisan make:model Flight --migration

11

Sau khi xác định và ánh xạ các sự kiện Eloquent của bạn, bạn có thể sử dụng các trình lắng nghe sự kiện để xử lý các sự kiện

Cảnh báo
Khi phát hành truy vấn cập nhật hoặc xóa hàng loạt qua Eloquent, các sự kiện mô hình

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

36,

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

38,

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

91 và

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

92 sẽ không được gửi cho các mô hình bị ảnh hưởng. Điều này là do các mô hình không bao giờ thực sự được truy xuất khi thực hiện cập nhật hoặc xóa hàng loạt.

Sử dụng Closures

Thay vì sử dụng các lớp sự kiện tùy chỉnh, bạn có thể đăng ký các lần đóng để thực thi khi các sự kiện mô hình khác nhau được gửi đi. Thông thường, bạn nên đăng ký các bao đóng này trong phương pháp

php artisan make:model Flight --migration

045 của mô hình của bạn

php artisan make:model Flight --migration

12

Nếu cần, bạn có thể sử dụng trình xử lý sự kiện ẩn danh có thể xếp hàng đợi khi đăng ký sự kiện mô hình. Điều này sẽ hướng dẫn Laravel thực thi trình lắng nghe sự kiện mô hình trong nền bằng cách sử dụng hàng đợi của ứng dụng của bạn

php artisan make:model Flight --migration

13

quan sát viên

Xác định người quan sát

Nếu bạn đang lắng nghe nhiều sự kiện trên một mô hình nhất định, bạn có thể sử dụng bộ quan sát để nhóm tất cả người nghe của mình vào một lớp duy nhất. Các lớp Người quan sát có các tên phương thức phản ánh các sự kiện Eloquent mà bạn muốn lắng nghe. Mỗi phương thức này nhận mô hình bị ảnh hưởng làm đối số duy nhất của chúng. Lệnh

php artisan make:model Flight --migration

097 Artisan là cách dễ nhất để tạo một lớp quan sát viên mới

php artisan make:model Flight --migration

14

Lệnh này sẽ đặt người quan sát mới vào thư mục

php artisan make:model Flight --migration

098 của bạn. Nếu thư mục này không tồn tại, Artisan sẽ tạo nó cho bạn. Người quan sát mới của bạn sẽ giống như sau

php artisan make:model Flight --migration

15

Để đăng ký một người quan sát, bạn cần gọi phương thức

php artisan make:model Flight --migration

099 trên mô hình mà bạn muốn quan sát. Bạn có thể đăng ký người quan sát theo phương pháp

php artisan make:model Flight --migration

39 của nhà cung cấp dịch vụ

php artisan make:model Flight --migration

301 của ứng dụng của bạn

php artisan make:model Flight --migration

16

Ngoài ra, bạn có thể liệt kê các quan sát viên của mình trong một thuộc tính

php artisan make:model Flight --migration

302 của lớp

php artisan make:model Flight --migration

301 của ứng dụng của bạn

php artisan make:model Flight --migration

17

Lưu ý
Có những sự kiện bổ sung mà người quan sát có thể nghe, chẳng hạn như

# Generate a model and a FlightFactory class...

php artisan make:model Flight --factory

php artisan make:model Flight -f

# Generate a model and a FlightSeeder class...

php artisan make:model Flight --seed

php artisan make:model Flight -s

# Generate a model and a FlightController class...

php artisan make:model Flight --controller

php artisan make:model Flight -c

# Generate a model, FlightController resource class, and form request classes...

php artisan make:model Flight --controller --resource --requests

php artisan make:model Flight -crR

# Generate a model and a FlightPolicy class...

php artisan make:model Flight --policy

# Generate a model and a migration, factory, seeder, and controller...

php artisan make:model Flight -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...

php artisan make:model Flight --all

# Generate a pivot model...

php artisan make:model Member --pivot

35 và

php artisan make:model Flight --migration

067. Những sự kiện này được mô tả trong tài liệu sự kiện.

Người quan sát & Giao dịch cơ sở dữ liệu

Khi các mô hình đang được tạo trong một giao dịch cơ sở dữ liệu, bạn có thể muốn hướng dẫn một người quan sát chỉ thực hiện các trình xử lý sự kiện của nó sau khi giao dịch cơ sở dữ liệu được thực hiện. Bạn có thể thực hiện điều này bằng cách xác định một thuộc tính

php artisan make:model Flight --migration

306 trên người quan sát. Nếu một giao dịch cơ sở dữ liệu không được tiến hành, các trình xử lý sự kiện sẽ thực thi ngay lập tức

php artisan make:model Flight --migration

18

Sự kiện tắt tiếng

Đôi khi, bạn có thể cần tạm thời "tắt tiếng" tất cả các sự kiện được kích hoạt bởi một mô hình. Bạn có thể đạt được điều này bằng cách sử dụng phương pháp

php artisan make:model Flight --migration

307. Phương thức

php artisan make:model Flight --migration

307 chấp nhận một bao đóng làm đối số duy nhất của nó. Bất kỳ mã nào được thực thi trong lần đóng này sẽ không gửi các sự kiện mô hình và mọi giá trị được trả về bởi lần đóng sẽ được trả về bằng phương thức

php artisan make:model Flight --migration

307

php artisan make:model Flight --migration

19

Lưu một mô hình duy nhất mà không có sự kiện

Đôi khi bạn có thể muốn "lưu" một mô hình nhất định mà không gửi bất kỳ sự kiện nào. Bạn có thể thực hiện việc này bằng cách sử dụng phương pháp

php artisan make:model Flight --migration

310

php artisan make:model Flight --migration

00

Bạn cũng có thể "cập nhật", "xóa", "xóa mềm", "khôi phục" và "sao chép" một mô hình nhất định mà không gửi bất kỳ sự kiện nào