Hàm tổng hợp trong Laravel Eloquent

Laravel Eloquent cho phép bạn truy vấn số lượng của một mối quan hệ bằng cách sử dụng

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

79. Aggregate mở rộng Eloquent bằng cách thêm

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

80,

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

81,

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 và

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

83

Điều này dựa trên công việc trong

$orders = Order::withSum['products', 'quantity']->get[];

$orders->each[function [$order] {
    $order->products_sum;
}];
0 - cảm ơn Mohammad Sharif Ahrari [@spyp]

Cài đặt

Bạn có thể cài đặt gói qua Composer

composer require watson/aggregate

Cách sử dụng

Các phương thức bổ sung sẽ được bổ sung bởi tính năng khám phá tự động của Laravel. Sau đó, bạn có thể sử dụng chúng giống như cách bạn đã sử dụng

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

79. Xem tài liệu về Laravel để biết thêm về cách thức hoạt động của nó

Các bảng cơ sở dữ liệu thường liên quan đến nhau. Ví dụ: một bài đăng trên blog có thể có nhiều bình luận hoặc một đơn đặt hàng có thể liên quan đến người dùng đã đặt nó. Eloquent làm cho việc quản lý và làm việc với các mối quan hệ này trở nên dễ dàng và hỗ trợ nhiều loại mối quan hệ phổ biến

Xác định mối quan hệ

Các mối quan hệ Eloquent được định nghĩa là các phương thức trên các lớp mô hình Eloquent của bạn. Vì các mối quan hệ cũng đóng vai trò là trình tạo truy vấn mạnh mẽ, nên việc xác định các mối quan hệ dưới dạng phương thức cung cấp khả năng truy vấn và xâu chuỗi phương thức mạnh mẽ. Ví dụ: chúng tôi có thể xâu chuỗi các ràng buộc truy vấn bổ sung trên mối quan hệ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

34 này

$user->posts[]->where['active', 1]->get[];

Tuy nhiên, trước khi đi sâu vào việc sử dụng các mối quan hệ, hãy tìm hiểu cách xác định từng loại mối quan hệ được hỗ trợ bởi Eloquent

Một Đối Một

Mối quan hệ một đối một là một kiểu quan hệ cơ sở dữ liệu rất cơ bản. Ví dụ: một mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

35 có thể được liên kết với một mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

36. Để xác định mối quan hệ này, chúng tôi sẽ đặt một phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

37 trên mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

35. Phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

37 nên gọi phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

40 và trả về kết quả của nó. Phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

40 có sẵn cho mô hình của bạn thông qua lớp cơ sở

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

42 của mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

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

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

40 là tên của lớp mô hình liên quan. Khi mối quan hệ được xác định, chúng tôi có thể truy xuất bản ghi liên quan bằng các thuộc tính động của Eloquent. Các thuộc tính động cho phép bạn truy cập các phương thức quan hệ như thể chúng là các thuộc tính được xác định trên mô hình

composer require watson/aggregate
4

Eloquent xác định khóa ngoại của quan hệ dựa trên tên model gốc. Trong trường hợp này, mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

36 tự động được coi là có khóa ngoại

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

45. Nếu bạn muốn ghi đè quy ước này, bạn có thể chuyển đối số thứ hai cho phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

40

composer require watson/aggregate
8

Ngoài ra, Eloquent giả định rằng khóa ngoại phải có giá trị khớp với cột khóa chính của khóa gốc. Nói cách khác, Eloquent sẽ tìm kiếm giá trị của cột

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

47 của người dùng trong cột

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

45 của bản ghi

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

36. Nếu bạn muốn mối quan hệ sử dụng giá trị khóa chính khác với

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

47 hoặc thuộc tính

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

51 của mô hình của bạn, bạn có thể chuyển đối số thứ ba cho phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

40

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

5

Xác định nghịch đảo của mối quan hệ

Vì vậy, chúng tôi có thể truy cập mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

36 từ mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

35 của chúng tôi. Tiếp theo, hãy xác định mối quan hệ trên mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

36 sẽ cho phép chúng tôi truy cập người dùng sở hữu điện thoại. Chúng ta có thể định nghĩa nghịch đảo của mối quan hệ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

40 bằng cách sử dụng phương pháp

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

57

$user->posts[]->where['active', 1]->get[];

1

Khi gọi phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

58, Eloquent sẽ cố gắng tìm một mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

35 có một

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

47 khớp với cột

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

45 trên mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

36

Eloquent xác định tên khóa ngoại bằng cách kiểm tra tên của phương thức quan hệ và thêm hậu tố tên phương thức là

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

63. Vì vậy, trong trường hợp này, Eloquent giả định rằng mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

36 có cột

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

45. Tuy nhiên, nếu khóa ngoại trên mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

36 không phải là

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

45, bạn có thể chuyển tên khóa tùy chỉnh làm đối số thứ hai cho phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

57

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

3

Nếu mô hình gốc không sử dụng

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

47 làm khóa chính hoặc bạn muốn tìm mô hình được liên kết bằng một cột khác, bạn có thể chuyển đối số thứ ba cho phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

57 chỉ định khóa tùy chỉnh của bảng cha

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

6

một đến nhiều

Mối quan hệ một-nhiều được sử dụng để xác định các mối quan hệ trong đó một mô hình đơn lẻ là cha của một hoặc nhiều mô hình con. Ví dụ: một bài đăng trên blog có thể có vô số bình luận. Giống như tất cả các mối quan hệ Eloquent khác, mối quan hệ một-nhiều được xác định bằng cách xác định một phương thức trên mô hình Eloquent của bạn

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

7

Hãy nhớ rằng, Eloquent sẽ tự động xác định cột khóa ngoại thích hợp cho mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

71. Theo quy ước, Eloquent sẽ lấy tên "snake case" của model gốc và thêm vào hậu tố là

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

63. Vì vậy, trong ví dụ này, Eloquent sẽ giả sử cột khóa ngoại trên mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

71 là

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

74

Khi phương thức quan hệ đã được xác định, chúng ta có thể truy cập vào bộ sưu tập các nhận xét có liên quan bằng cách truy cập thuộc tính

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

75. Hãy nhớ rằng, vì Eloquent cung cấp "các thuộc tính quan hệ động", nên chúng ta có thể truy cập các phương thức quan hệ như thể chúng được định nghĩa là các thuộc tính trên model

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

3

Vì tất cả các mối quan hệ cũng đóng vai trò là trình tạo truy vấn, bạn có thể thêm các ràng buộc khác vào truy vấn mối quan hệ bằng cách gọi phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

75 và tiếp tục xâu chuỗi các điều kiện vào truy vấn

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

0

Giống như phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

40, bạn cũng có thể ghi đè khóa ngoại và khóa cục bộ bằng cách chuyển các đối số bổ sung cho phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

78

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

1

Một Đến Nhiều [Nghịch Đảo] / Thuộc Về

Bây giờ chúng ta có thể truy cập tất cả các bình luận của bài đăng, hãy xác định mối quan hệ để cho phép một bình luận truy cập bài đăng gốc của nó. Để xác định nghịch đảo của mối quan hệ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

78, hãy xác định phương thức quan hệ trên mô hình con gọi phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

57

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

2

Khi mối quan hệ đã được xác định, chúng tôi có thể truy xuất bài đăng gốc của nhận xét bằng cách truy cập "thuộc tính mối quan hệ động" của

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

81

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

3

Trong ví dụ trên, Eloquent sẽ cố gắng tìm một mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 có một

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

47 khớp với cột

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

74 trên mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

71

Eloquent xác định tên khóa ngoại mặc định bằng cách kiểm tra tên của phương thức quan hệ và thêm vào hậu tố tên phương thức một

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

86 theo sau là tên của cột khóa chính của mô hình gốc. Vì vậy, trong ví dụ này, Eloquent sẽ giả sử khóa ngoại của mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 trên bảng

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

75 là

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

74

Tuy nhiên, nếu khóa ngoại cho mối quan hệ của bạn không tuân theo các quy ước này, bạn có thể chuyển tên khóa ngoại tùy chỉnh làm đối số thứ hai cho phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

57

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

4

Nếu mô hình gốc của bạn không sử dụng

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

47 làm khóa chính hoặc bạn muốn tìm mô hình được liên kết bằng một cột khác, bạn có thể chuyển đối số thứ ba cho phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

57 chỉ định khóa tùy chỉnh của bảng cha mẹ của bạn

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

5

Mô hình mặc định

Các mối quan hệ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

57,

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

40,

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

95 và

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

96 cho phép bạn xác định một mô hình mặc định sẽ được trả về nếu mối quan hệ đã cho là

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

97. Mẫu này thường được gọi là mẫu Đối tượng Null và có thể giúp loại bỏ các kiểm tra có điều kiện trong mã của bạn. Trong ví dụ sau, quan hệ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

58 sẽ trả về một mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

99 trống nếu không có người dùng nào được đính kèm vào mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

6

Để điền các thuộc tính vào mô hình mặc định, bạn có thể truyền một mảng hoặc bao đóng cho phương thức

composer require watson/aggregate
401

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

7

Truy vấn thuộc về các mối quan hệ

Khi truy vấn các phần tử con của mối quan hệ "thuộc về", bạn có thể xây dựng mệnh đề

composer require watson/aggregate
402 theo cách thủ công để truy xuất các mô hình Eloquent tương ứng

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

8

Tuy nhiên, bạn có thể thấy thuận tiện hơn khi sử dụng phương pháp

composer require watson/aggregate
403, phương pháp này sẽ tự động xác định mối quan hệ thích hợp và khóa ngoại cho mô hình đã cho

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

9

Bạn cũng có thể cung cấp một thể hiện bộ sưu tập cho phương thức

composer require watson/aggregate
403. Khi làm như vậy, Laravel sẽ truy xuất các mô hình thuộc về bất kỳ mô hình cha nào trong bộ sưu tập

composer require watson/aggregate
40

Theo mặc định, Laravel sẽ xác định mối quan hệ được liên kết với mô hình đã cho dựa trên tên lớp của mô hình;

composer require watson/aggregate
41

Có Một Trong Nhiều

Đôi khi một mô hình có thể có nhiều mô hình liên quan, nhưng bạn muốn dễ dàng truy xuất mô hình liên quan "mới nhất" hoặc "cũ nhất" của mối quan hệ. Ví dụ: một mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

35 có thể liên quan đến nhiều mô hình
composer require watson/aggregate
407, nhưng bạn muốn xác định một cách thuận tiện để tương tác với đơn hàng gần đây nhất mà người dùng đã đặt. Bạn có thể thực hiện điều này bằng cách sử dụng loại mối quan hệ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

40 kết hợp với các phương pháp
composer require watson/aggregate
409

composer require watson/aggregate
42

Tương tự như vậy, bạn có thể xác định một phương thức để truy xuất mô hình liên quan "cũ nhất" hoặc đầu tiên của một mối quan hệ

composer require watson/aggregate
43

Theo mặc định, các phương thức

composer require watson/aggregate
410 và
composer require watson/aggregate
411 sẽ truy xuất mô hình liên quan mới nhất hoặc cũ nhất dựa trên khóa chính của mô hình, khóa này phải có thể sắp xếp được. Tuy nhiên, đôi khi bạn có thể muốn truy xuất một mô hình từ một mối quan hệ lớn hơn bằng cách sử dụng tiêu chí sắp xếp khác

Ví dụ: sử dụng phương pháp

composer require watson/aggregate
409, bạn có thể truy xuất đơn đặt hàng đắt nhất của người dùng. Phương thức
composer require watson/aggregate
409 chấp nhận cột có thể sắp xếp làm đối số đầu tiên của nó và hàm tổng hợp nào [
composer require watson/aggregate
414 hoặc
composer require watson/aggregate
415] sẽ áp dụng khi truy vấn mô hình liên quan

composer require watson/aggregate
44

Cảnh báo
Vì PostgreSQL không hỗ trợ thực thi hàm

composer require watson/aggregate
416 đối với các cột UUID nên hiện tại không thể sử dụng mối quan hệ một trong nhiều kết hợp với các cột UUID của PostgreSQL.

Nâng cao có một trong nhiều mối quan hệ

Có thể xây dựng các mối quan hệ "có một trong nhiều" nâng cao hơn. Ví dụ: mô hình

composer require watson/aggregate
417 có thể có nhiều mô hình
composer require watson/aggregate
418 được liên kết được giữ lại trong hệ thống ngay cả sau khi giá mới được công bố. Ngoài ra, dữ liệu giá mới cho sản phẩm có thể được xuất bản trước để có hiệu lực vào một ngày trong tương lai thông qua cột
composer require watson/aggregate
419

Vì vậy, tóm lại, chúng tôi cần truy xuất giá được xuất bản mới nhất khi ngày xuất bản không phải là trong tương lai. Ngoài ra, nếu hai giá có cùng ngày xuất bản, chúng tôi sẽ ưu tiên giá có ID lớn nhất. Để thực hiện điều này, chúng ta phải chuyển một mảng tới phương thức

composer require watson/aggregate
409 có chứa các cột có thể sắp xếp để xác định giá mới nhất. Ngoài ra, một bao đóng sẽ được cung cấp làm đối số thứ hai cho phương thức
composer require watson/aggregate
409. Việc đóng này sẽ chịu trách nhiệm thêm các ràng buộc ngày xuất bản bổ sung vào truy vấn mối quan hệ

composer require watson/aggregate
45

Có Một Thông Qua

Mối quan hệ "có một lần" xác định mối quan hệ một-một với một mô hình khác. Tuy nhiên, mối quan hệ này chỉ ra rằng mô hình khai báo có thể được khớp với một phiên bản của mô hình khác bằng cách tiếp tục thông qua mô hình thứ ba

Ví dụ: trong một ứng dụng cửa hàng sửa chữa xe, mỗi kiểu máy

composer require watson/aggregate
422 có thể được liên kết với một kiểu máy
composer require watson/aggregate
423 và mỗi kiểu máy
composer require watson/aggregate
423 có thể được liên kết với một kiểu máy
composer require watson/aggregate
425. Mặc dù thợ máy và chủ sở hữu không có mối quan hệ trực tiếp trong cơ sở dữ liệu, nhưng thợ máy có thể truy cập chủ sở hữu thông qua mô hình
composer require watson/aggregate
423. Hãy xem các bảng cần thiết để xác định mối quan hệ này

Bây giờ chúng ta đã kiểm tra cấu trúc bảng cho mối quan hệ, hãy xác định mối quan hệ trên mô hình

composer require watson/aggregate
422

composer require watson/aggregate
46

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

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

95 là tên của mô hình cuối cùng mà chúng tôi muốn truy cập, trong khi đối số thứ hai là tên của mô hình trung gian

Công ước chính

Các quy ước khóa ngoại điển hình của Eloquent sẽ được sử dụng khi thực hiện các truy vấn của mối quan hệ. Nếu bạn muốn tùy chỉnh các khóa của mối quan hệ, bạn có thể chuyển chúng làm đối số thứ ba và thứ tư cho phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

95. Đối số thứ ba là tên của khóa ngoại trên mô hình trung gian. Đối số thứ tư là tên của khóa ngoại trên mô hình cuối cùng. Đối số thứ năm là khóa cục bộ, trong khi đối số thứ sáu là khóa cục bộ của mô hình trung gian

composer require watson/aggregate
47

Có Nhiều Thông Qua

Mối quan hệ "có nhiều thông qua" cung cấp một cách thuận tiện để truy cập các mối quan hệ ở xa thông qua một mối quan hệ trung gian. Ví dụ: giả sử chúng ta đang xây dựng một nền tảng triển khai như Laravel Vapor. Mô hình

composer require watson/aggregate
430 có thể truy cập nhiều mô hình
composer require watson/aggregate
431 thông qua mô hình trung gian
composer require watson/aggregate
432. Sử dụng ví dụ này, bạn có thể dễ dàng thu thập tất cả các triển khai cho một dự án nhất định. Hãy xem các bảng cần thiết để xác định mối quan hệ này

Bây giờ chúng ta đã kiểm tra cấu trúc bảng cho mối quan hệ, hãy xác định mối quan hệ trên mô hình

composer require watson/aggregate
430

composer require watson/aggregate
48

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

composer require watson/aggregate
434 là tên của mô hình cuối cùng mà chúng tôi muốn truy cập, trong khi đối số thứ hai là tên của mô hình trung gian

Mặc dù bảng của mô hình

composer require watson/aggregate
431 không chứa cột
composer require watson/aggregate
436, nhưng mối quan hệ
composer require watson/aggregate
434 cung cấp quyền truy cập vào các triển khai của dự án thông qua
composer require watson/aggregate
438. Để truy xuất các mô hình này, Eloquent kiểm tra cột
composer require watson/aggregate
436 trên bảng của mô hình trung gian
composer require watson/aggregate
432. Sau khi tìm thấy ID môi trường có liên quan, chúng được sử dụng để truy vấn bảng của mô hình
composer require watson/aggregate
431

Công ước chính

Các quy ước khóa ngoại điển hình của Eloquent sẽ được sử dụng khi thực hiện các truy vấn của mối quan hệ. Nếu bạn muốn tùy chỉnh các khóa của mối quan hệ, bạn có thể chuyển chúng làm đối số thứ ba và thứ tư cho phương thức

composer require watson/aggregate
434. Đối số thứ ba là tên của khóa ngoại trên mô hình trung gian. Đối số thứ tư là tên của khóa ngoại trên mô hình cuối cùng. Đối số thứ năm là khóa cục bộ, trong khi đối số thứ sáu là khóa cục bộ của mô hình trung gian

composer require watson/aggregate
49

Nhiều đến nhiều mối quan hệ

Mối quan hệ nhiều-nhiều phức tạp hơn một chút so với mối quan hệ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

40 và

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

78. Một ví dụ về mối quan hệ nhiều-nhiều là một người dùng có nhiều vai trò và những vai trò đó cũng được chia sẻ bởi những người dùng khác trong ứng dụng. Ví dụ: người dùng có thể được chỉ định vai trò "Tác giả" và "Biên tập viên"; . Vì vậy, một người dùng có nhiều vai trò và một vai trò có nhiều người dùng

Cấu trúc bảng

Để xác định mối quan hệ này, cần có ba bảng cơ sở dữ liệu.

composer require watson/aggregate
445,
composer require watson/aggregate
446 và
composer require watson/aggregate
447. Bảng
composer require watson/aggregate
447 được lấy từ thứ tự bảng chữ cái của tên kiểu máy có liên quan và chứa các cột

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

45 và
composer require watson/aggregate
450. Bảng này được sử dụng làm bảng trung gian liên kết người dùng và vai trò

Hãy nhớ rằng, vì một vai trò có thể thuộc về nhiều người dùng, nên chúng ta không thể chỉ đặt một cột

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

45 trên bảng
composer require watson/aggregate
446. Điều này có nghĩa là một vai trò chỉ có thể thuộc về một người dùng. Để cung cấp hỗ trợ cho các vai trò được gán cho nhiều người dùng, bảng
composer require watson/aggregate
447 là cần thiết. Chúng ta có thể tóm tắt cấu trúc bảng của mối quan hệ như vậy

Cấu trúc mô hình

Mối quan hệ nhiều-nhiều được xác định bằng cách viết một phương thức trả về kết quả của phương thức

composer require watson/aggregate
454. Phương thức
composer require watson/aggregate
454 được cung cấp bởi lớp cơ sở

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

42 được sử dụng bởi tất cả các mô hình Eloquent trong ứng dụng của bạn. Ví dụ: hãy xác định phương thức
composer require watson/aggregate
446 trên mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

35 của chúng tôi. Đối số đầu tiên được truyền cho phương thức này là tên của lớp mô hình liên quan

composer require watson/aggregate
80

Khi mối quan hệ được xác định, bạn có thể truy cập vai trò của người dùng bằng thuộc tính mối quan hệ động

composer require watson/aggregate
446

composer require watson/aggregate
81

Vì tất cả các mối quan hệ cũng đóng vai trò là trình tạo truy vấn, nên bạn có thể thêm các ràng buộc khác vào truy vấn mối quan hệ bằng cách gọi phương thức

composer require watson/aggregate
446 và tiếp tục xâu chuỗi các điều kiện vào truy vấn

composer require watson/aggregate
82

Để xác định tên bảng của bảng trung gian của mối quan hệ, Eloquent sẽ nối hai tên mô hình có liên quan theo thứ tự bảng chữ cái. Tuy nhiên, bạn có thể tự do ghi đè quy ước này. Bạn có thể làm như vậy bằng cách chuyển đối số thứ hai cho phương thức

composer require watson/aggregate
454

composer require watson/aggregate
83

Ngoài việc tùy chỉnh tên của bảng trung gian, bạn cũng có thể tùy chỉnh tên cột của các khóa trên bảng bằng cách chuyển các đối số bổ sung cho phương thức

composer require watson/aggregate
454. Đối số thứ ba là tên khóa ngoại của mô hình mà bạn đang xác định mối quan hệ, trong khi đối số thứ tư là tên khóa ngoại của mô hình mà bạn đang tham gia

composer require watson/aggregate
84

Xác định nghịch đảo của mối quan hệ

Để xác định "nghịch đảo" của mối quan hệ nhiều-nhiều, bạn nên xác định một phương thức trên mô hình liên quan, phương thức này cũng trả về kết quả của phương thức

composer require watson/aggregate
454. Để hoàn thành ví dụ về vai trò/người dùng của chúng tôi, hãy xác định phương thức
composer require watson/aggregate
445 trên mô hình
composer require watson/aggregate
465

composer require watson/aggregate
85

Như bạn có thể thấy, mối quan hệ được xác định chính xác giống như đối tác mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

35 của nó, ngoại trừ việc tham chiếu mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

99. Vì chúng tôi đang sử dụng lại phương pháp
composer require watson/aggregate
454, nên tất cả các tùy chọn tùy chỉnh khóa và bảng thông thường đều khả dụng khi xác định "nghịch đảo" của mối quan hệ nhiều-nhiều

Truy xuất các cột trong bảng trung gian

Như bạn đã biết, làm việc với quan hệ nhiều-nhiều yêu cầu phải có bảng trung gian. Eloquent cung cấp một số cách rất hữu ích để tương tác với bảng này. Ví dụ: giả sử mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

35 của chúng tôi có nhiều mô hình
composer require watson/aggregate
465 liên quan đến. Sau khi truy cập vào mối quan hệ này, chúng ta có thể truy cập vào bảng trung gian bằng cách sử dụng thuộc tính
composer require watson/aggregate
471 trên các mô hình

composer require watson/aggregate
86

Lưu ý rằng mỗi mô hình

composer require watson/aggregate
465 mà chúng tôi truy xuất được tự động gán một thuộc tính
composer require watson/aggregate
471. Thuộc tính này chứa một mô hình đại diện cho bảng trung gian

Theo mặc định, chỉ các phím mô hình sẽ có mặt trên mô hình

composer require watson/aggregate
471. Nếu bảng trung gian của bạn chứa các thuộc tính bổ sung, bạn phải chỉ định chúng khi xác định mối quan hệ

composer require watson/aggregate
87

Nếu bạn muốn bảng trung gian của mình có dấu thời gian

composer require watson/aggregate
475 và
composer require watson/aggregate
476 được Eloquent tự động duy trì, hãy gọi phương thức
composer require watson/aggregate
477 khi xác định mối quan hệ

composer require watson/aggregate
88

Cảnh báo
Các bảng trung gian sử dụng dấu thời gian được duy trì tự động của Eloquent bắt buộc phải có cả hai cột dấu thời gian

composer require watson/aggregate
475 và
composer require watson/aggregate
476.

Tùy chỉnh tên thuộc tính
composer require watson/aggregate
471

Như đã lưu ý trước đây, các thuộc tính từ bảng trung gian có thể được truy cập trên các mô hình thông qua thuộc tính

composer require watson/aggregate
471. Tuy nhiên, bạn có thể tự do tùy chỉnh tên của thuộc tính này để phản ánh tốt hơn mục đích của nó trong ứng dụng của bạn

Ví dụ: nếu ứng dụng của bạn chứa người dùng có thể đăng ký podcast, bạn có thể có mối quan hệ nhiều-nhiều giữa người dùng và podcast. Nếu đúng như vậy, bạn có thể muốn đổi tên thuộc tính bảng trung gian của mình thành

composer require watson/aggregate
482 thay vì
composer require watson/aggregate
471. Điều này có thể được thực hiện bằng cách sử dụng phương pháp
composer require watson/aggregate
484 khi xác định mối quan hệ

composer require watson/aggregate
89

Khi thuộc tính bảng trung gian tùy chỉnh đã được chỉ định, bạn có thể truy cập dữ liệu bảng trung gian bằng tên tùy chỉnh

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

50

Lọc truy vấn qua cột bảng trung gian

Bạn cũng có thể lọc các kết quả được trả về bởi truy vấn mối quan hệ

composer require watson/aggregate
454 bằng cách sử dụng các phương pháp
composer require watson/aggregate
486,
composer require watson/aggregate
487,
composer require watson/aggregate
488,
composer require watson/aggregate
489,
composer require watson/aggregate
490,
composer require watson/aggregate
491 và
composer require watson/aggregate
492 khi xác định mối quan hệ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

51

Đặt hàng truy vấn qua cột bảng trung gian

Bạn có thể sắp xếp các kết quả được trả về bởi truy vấn mối quan hệ

composer require watson/aggregate
454 bằng phương pháp
composer require watson/aggregate
494. Trong ví dụ sau, chúng tôi sẽ truy xuất tất cả các huy hiệu mới nhất cho người dùng

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

52

Xác định các mô hình bảng trung gian tùy chỉnh

Nếu bạn muốn xác định một mô hình tùy chỉnh để biểu thị bảng trung gian của mối quan hệ nhiều-nhiều, bạn có thể gọi phương thức

composer require watson/aggregate
495 khi xác định mối quan hệ. Các mô hình trục tùy chỉnh cung cấp cho bạn cơ hội xác định hành vi bổ sung trên mô hình trục, chẳng hạn như các phương thức và phôi

Các mô hình trục nhiều-nhiều tùy chỉnh sẽ mở rộng lớp

composer require watson/aggregate
496 trong khi các mô hình trục nhiều-nhiều đa hình tùy chỉnh sẽ mở rộng lớp
composer require watson/aggregate
497. Ví dụ: chúng tôi có thể xác định mô hình
composer require watson/aggregate
465 sử dụng mô hình trục
composer require watson/aggregate
499 tùy chỉnh

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

53

Khi định nghĩa mô hình

composer require watson/aggregate
499, bạn nên mở rộng lớp
composer require watson/aggregate
496

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

54

Cảnh báo
Mô hình xoay vòng không được sử dụng đặc điểm

composer require watson/aggregate
802. Nếu bạn cần xóa mềm các bản ghi trục, hãy xem xét chuyển đổi mô hình trục của bạn thành một mô hình Eloquent thực tế.

Mô hình xoay vòng tùy chỉnh và ID tăng dần

Nếu bạn đã xác định mối quan hệ nhiều-nhiều sử dụng mô hình trục tùy chỉnh và mô hình trục đó có khóa chính tự động tăng, thì bạn nên đảm bảo rằng lớp mô hình trục tùy chỉnh của mình xác định thuộc tính

composer require watson/aggregate
803 được đặt thành
composer require watson/aggregate
804

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

55

Mối quan hệ đa hình

Mối quan hệ đa hình cho phép mô hình con thuộc về nhiều loại mô hình bằng cách sử dụng một liên kết duy nhất. Ví dụ: hãy tưởng tượng bạn đang xây dựng một ứng dụng cho phép người dùng chia sẻ các bài đăng trên blog và video. Trong một ứng dụng như vậy, một mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

71 có thể thuộc về cả hai mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 và
composer require watson/aggregate
807

Một Đối Một [Đa hình]

Cấu trúc bảng

Mối quan hệ đa hình một đối một tương tự như mối quan hệ một đối một điển hình; . Ví dụ: một blog

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 và một

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

35 có thể chia sẻ mối quan hệ đa hình với một mô hình
composer require watson/aggregate
810. Sử dụng mối quan hệ đa hình một đối một cho phép bạn có một bảng hình ảnh duy nhất có thể được liên kết với bài đăng và người dùng. Đầu tiên, hãy kiểm tra cấu trúc bảng

Lưu ý các cột

composer require watson/aggregate
811 và
composer require watson/aggregate
812 trên bảng
composer require watson/aggregate
813. Cột
composer require watson/aggregate
811 sẽ chứa giá trị ID của bài đăng hoặc người dùng, trong khi cột
composer require watson/aggregate
812 sẽ chứa tên lớp của mô hình gốc. Cột
composer require watson/aggregate
812 được Eloquent sử dụng để xác định "loại" mô hình gốc nào sẽ trả về khi truy cập vào quan hệ
composer require watson/aggregate
817. Trong trường hợp này, cột sẽ chứa
composer require watson/aggregate
818 hoặc

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

99

Cấu trúc mô hình

Tiếp theo, hãy xem xét các định nghĩa mô hình cần thiết để xây dựng mối quan hệ này

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

56

Truy xuất mối quan hệ

Khi bảng cơ sở dữ liệu và các mô hình của bạn được xác định, bạn có thể truy cập các mối quan hệ thông qua các mô hình của mình. Ví dụ: để truy xuất hình ảnh cho một bài đăng, chúng ta có thể truy cập thuộc tính quan hệ động

composer require watson/aggregate
820

Bạn có thể truy xuất cha của mô hình đa hình bằng cách truy cập tên của phương thức thực hiện lệnh gọi tới

composer require watson/aggregate
821. Trong trường hợp này, đó là phương pháp
composer require watson/aggregate
817 trên mô hình
composer require watson/aggregate
810. Vì vậy, chúng tôi sẽ truy cập phương thức đó dưới dạng thuộc tính mối quan hệ động

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

57

Mối quan hệ

composer require watson/aggregate
817 trên mô hình
composer require watson/aggregate
810 sẽ trả về một thể hiện

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 hoặc

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

35, tùy thuộc vào loại mô hình sở hữu hình ảnh

Công ước chính

Nếu cần, bạn có thể chỉ định tên của cột "id" và "type" được mô hình con đa hình của bạn sử dụng. Nếu bạn làm như vậy, hãy đảm bảo rằng bạn luôn chuyển tên của mối quan hệ làm đối số đầu tiên cho phương thức

composer require watson/aggregate
821. Thông thường, giá trị này phải khớp với tên phương thức, vì vậy bạn có thể sử dụng hằng số
composer require watson/aggregate
829 của PHP

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

58

Một Đến Nhiều [Đa hình]

Cấu trúc bảng

Mối quan hệ đa hình một-nhiều tương tự như mối quan hệ một-nhiều điển hình; . Ví dụ: hãy tưởng tượng người dùng ứng dụng của bạn có thể "nhận xét" về bài đăng và video. Sử dụng các mối quan hệ đa hình, bạn có thể sử dụng một bảng

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

75 duy nhất để chứa nhận xét cho cả bài đăng và video. Đầu tiên, hãy kiểm tra cấu trúc bảng cần thiết để xây dựng mối quan hệ này

Cấu trúc mô hình

Tiếp theo, hãy xem xét các định nghĩa mô hình cần thiết để xây dựng mối quan hệ này

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

59

Truy xuất mối quan hệ

Khi bảng cơ sở dữ liệu và các mô hình của bạn được xác định, bạn có thể truy cập các mối quan hệ thông qua các thuộc tính mối quan hệ động của mô hình của bạn. Ví dụ: để truy cập tất cả các nhận xét cho một bài đăng, chúng ta có thể sử dụng thuộc tính động

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

75

$user->posts[]->where['active', 1]->get[];

10

Bạn cũng có thể truy xuất cha của một mô hình con đa hình bằng cách truy cập tên của phương thức thực hiện lệnh gọi tới

composer require watson/aggregate
821. Trong trường hợp này, đó là phương pháp
composer require watson/aggregate
833 trên mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

71. Vì vậy, chúng tôi sẽ truy cập phương thức đó dưới dạng thuộc tính quan hệ động để truy cập mô hình gốc của nhận xét

$user->posts[]->where['active', 1]->get[];

11

Mối quan hệ

composer require watson/aggregate
833 trên mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

71 sẽ trả về một thể hiện

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 hoặc
composer require watson/aggregate
807, tùy thuộc vào loại mô hình nào là cha của nhận xét

Một trong nhiều [Đa hình]

Đôi khi một mô hình có thể có nhiều mô hình liên quan, nhưng bạn muốn dễ dàng truy xuất mô hình liên quan "mới nhất" hoặc "cũ nhất" của mối quan hệ. Ví dụ: một mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

35 có thể liên quan đến nhiều mô hình
composer require watson/aggregate
810, nhưng bạn muốn xác định một cách thuận tiện để tương tác với hình ảnh gần đây nhất mà người dùng đã tải lên. Bạn có thể thực hiện điều này bằng cách sử dụng loại mối quan hệ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

96 kết hợp với các phương pháp
composer require watson/aggregate
409

$user->posts[]->where['active', 1]->get[];

12

Tương tự như vậy, bạn có thể xác định một phương thức để truy xuất mô hình liên quan "cũ nhất" hoặc đầu tiên của một mối quan hệ

$user->posts[]->where['active', 1]->get[];

13

Theo mặc định, các phương thức

composer require watson/aggregate
410 và
composer require watson/aggregate
411 sẽ truy xuất mô hình liên quan mới nhất hoặc cũ nhất dựa trên khóa chính của mô hình, khóa này phải có thể sắp xếp được. Tuy nhiên, đôi khi bạn có thể muốn truy xuất một mô hình từ một mối quan hệ lớn hơn bằng cách sử dụng tiêu chí sắp xếp khác

Ví dụ: sử dụng phương pháp

composer require watson/aggregate
409, bạn có thể truy xuất hình ảnh được "thích" nhiều nhất của người dùng. Phương thức
composer require watson/aggregate
409 chấp nhận cột có thể sắp xếp làm đối số đầu tiên của nó và hàm tổng hợp nào [
composer require watson/aggregate
414 hoặc
composer require watson/aggregate
415] sẽ áp dụng khi truy vấn mô hình liên quan

$user->posts[]->where['active', 1]->get[];

14

Lưu ý
Có thể xây dựng mối quan hệ "một trong nhiều" nâng cao hơn. Để biết thêm thông tin, vui lòng tham khảo một trong nhiều tài liệu.

Nhiều Đến Nhiều [Đa hình]

Cấu trúc bảng

Mối quan hệ đa hình nhiều-nhiều phức tạp hơn một chút so với mối quan hệ "hình một" và "hình nhiều". Ví dụ: mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 và mô hình
composer require watson/aggregate
807 có thể chia sẻ mối quan hệ đa hình với mô hình
composer require watson/aggregate
851. Sử dụng mối quan hệ đa hình nhiều-nhiều trong tình huống này sẽ cho phép ứng dụng của bạn có một bảng các thẻ duy nhất có thể được liên kết với bài đăng hoặc video. Đầu tiên, hãy kiểm tra cấu trúc bảng cần thiết để xây dựng mối quan hệ này

Lưu ý
Trước khi đi sâu vào các mối quan hệ nhiều-nhiều đa hình, bạn có thể đọc tài liệu về các mối quan hệ nhiều-nhiều điển hình.

Cấu trúc mô hình

Tiếp theo, chúng tôi đã sẵn sàng để xác định các mối quan hệ trên các mô hình. Cả hai mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 và
composer require watson/aggregate
807 sẽ chứa một phương thức
composer require watson/aggregate
854 gọi phương thức
composer require watson/aggregate
855 được cung cấp bởi lớp mô hình Eloquent cơ sở

Phương thức

composer require watson/aggregate
855 chấp nhận tên của mô hình liên quan cũng như "tên mối quan hệ". Dựa trên tên mà chúng tôi đã gán cho tên bảng trung gian của mình và các khóa chứa trong đó, chúng tôi sẽ gọi mối quan hệ là "có thể gắn thẻ"

$user->posts[]->where['active', 1]->get[];

15

Xác định nghịch đảo của mối quan hệ

Tiếp theo, trên mô hình

composer require watson/aggregate
851, bạn nên xác định một phương thức cho từng mô hình gốc có thể có của nó. Vì vậy, trong ví dụ này, chúng ta sẽ định nghĩa một phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

34 và một phương thức
composer require watson/aggregate
859. Cả hai phương thức này sẽ trả về kết quả của phương thức
composer require watson/aggregate
860

Phương thức

composer require watson/aggregate
860 chấp nhận tên của mô hình liên quan cũng như "tên mối quan hệ". Dựa trên tên mà chúng tôi đã gán cho tên bảng trung gian của mình và các khóa chứa trong đó, chúng tôi sẽ gọi mối quan hệ là "có thể gắn thẻ"

$user->posts[]->where['active', 1]->get[];

16

Truy xuất mối quan hệ

Khi bảng cơ sở dữ liệu và các mô hình của bạn được xác định, bạn có thể truy cập các mối quan hệ thông qua các mô hình của mình. Ví dụ: để truy cập tất cả các thẻ cho một bài đăng, bạn có thể sử dụng thuộc tính quan hệ động

composer require watson/aggregate
854

$user->posts[]->where['active', 1]->get[];

17

Bạn có thể truy xuất cha của một quan hệ đa hình từ mô hình con đa hình bằng cách truy cập tên của phương thức thực hiện lệnh gọi tới

composer require watson/aggregate
860. Trong trường hợp này, đó là phương pháp

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

34 hoặc
composer require watson/aggregate
859 trên mô hình
composer require watson/aggregate
851

$user->posts[]->where['active', 1]->get[];

18

Các loại đa hình tùy chỉnh

Theo mặc định, Laravel sẽ sử dụng tên lớp đủ điều kiện để lưu trữ "loại" của mô hình liên quan. Chẳng hạn, với ví dụ về mối quan hệ một-nhiều ở trên, trong đó mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

71 có thể thuộc về mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 hoặc
composer require watson/aggregate
807, thì mặc định
composer require watson/aggregate
870 sẽ tương ứng là
composer require watson/aggregate
818 hoặc
composer require watson/aggregate
872. Tuy nhiên, bạn có thể muốn tách các giá trị này khỏi cấu trúc bên trong ứng dụng của mình

Ví dụ: thay vì sử dụng tên kiểu máy làm "loại", chúng tôi có thể sử dụng các chuỗi đơn giản như

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

81 và
composer require watson/aggregate
874. Bằng cách đó, các giá trị cột "loại" đa hình trong cơ sở dữ liệu của chúng tôi sẽ vẫn hợp lệ ngay cả khi các mô hình được đổi tên

$user->posts[]->where['active', 1]->get[];

19

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

composer require watson/aggregate
875 trong phương thức
composer require watson/aggregate
876 của lớp
composer require watson/aggregate
877 của bạn hoặc tạo một nhà cung cấp dịch vụ riêng nếu bạn muốn

Bạn có thể xác định bí danh hình thái của một mô hình nhất định trong thời gian chạy bằng cách sử dụng phương thức

composer require watson/aggregate
878 của mô hình. Ngược lại, bạn có thể xác định tên lớp đủ điều kiện được liên kết với bí danh hình thái bằng phương thức
composer require watson/aggregate
879

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

30

Cảnh báo
Khi thêm "bản đồ biến hình" vào ứng dụng hiện tại của bạn, mọi giá trị cột

composer require watson/aggregate
880 có thể biến đổi trong cơ sở dữ liệu của bạn vẫn chứa một lớp đủ tiêu chuẩn sẽ cần phải được chuyển đổi thành .

Mối quan hệ động

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

composer require watson/aggregate
881 để xác định mối quan hệ giữa các mô hình Eloquent trong thời gian chạy. Mặc dù thường không được đề xuất để phát triển ứng dụng thông thường, nhưng điều này đôi khi có thể hữu ích khi phát triển các gói Laravel

Phương thức

composer require watson/aggregate
881 chấp nhận tên mối quan hệ mong muốn làm đối số đầu tiên của nó. Đối số thứ hai được truyền cho phương thức phải là một bao đóng chấp nhận thể hiện của mô hình và trả về một định nghĩa mối quan hệ Eloquent hợp lệ. Thông thường, bạn nên định cấu hình các mối quan hệ động trong phương thức khởi động của nhà cung cấp dịch vụ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

31

Cảnh báo
Khi xác định mối quan hệ động, luôn cung cấp các đối số tên khóa rõ ràng cho các phương thức quan hệ Eloquent.

Quan hệ truy vấn

Vì tất cả các mối quan hệ của Eloquent đều được xác định thông qua các phương thức, nên bạn có thể gọi các phương thức đó để lấy một thể hiện của mối quan hệ mà không thực sự thực hiện truy vấn để tải các mô hình liên quan. Ngoài ra, tất cả các loại mối quan hệ Eloquent cũng đóng vai trò là trình tạo truy vấn, cho phép bạn tiếp tục xâu chuỗi các ràng buộc vào truy vấn mối quan hệ trước khi cuối cùng thực hiện truy vấn SQL đối với cơ sở dữ liệu của bạn

Ví dụ: hãy tưởng tượng một ứng dụng blog trong đó mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

35 có nhiều mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 được liên kết

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

32

Bạn có thể truy vấn mối quan hệ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

34 và thêm các ràng buộc bổ sung cho mối quan hệ như vậy

$user->posts[]->where['active', 1]->get[];

Bạn có thể sử dụng bất kỳ phương thức nào của trình tạo truy vấn Laravel trên mối quan hệ, vì vậy hãy đảm bảo khám phá tài liệu về trình tạo truy vấn để tìm hiểu về tất cả các phương pháp có sẵn cho bạn

Xâu chuỗi các mệnh đề
composer require watson/aggregate
886 sau các mối quan hệ

Như đã minh họa trong ví dụ trên, bạn có thể tự do thêm các ràng buộc bổ sung cho các mối quan hệ khi truy vấn chúng. Tuy nhiên, hãy thận trọng khi xâu chuỗi các mệnh đề _______ 1886 vào một mối quan hệ, vì các mệnh đề ________ 1886 sẽ được nhóm hợp lý ở cùng cấp độ với ràng buộc quan hệ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

34

Ví dụ trên sẽ tạo SQL sau. Như bạn có thể thấy, mệnh đề

composer require watson/aggregate
889 hướng dẫn truy vấn trả về bất kỳ người dùng nào có hơn 100 phiếu bầu. Truy vấn không còn bị hạn chế đối với một người dùng cụ thể

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

35

Trong hầu hết các trường hợp, bạn nên sử dụng các nhóm logic để nhóm các kiểm tra có điều kiện giữa các dấu ngoặc đơn

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

36

Ví dụ trên sẽ tạo ra SQL sau. Lưu ý rằng nhóm hợp lý đã nhóm đúng các ràng buộc và truy vấn vẫn bị ràng buộc đối với một người dùng cụ thể

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

37

Phương thức quan hệ Vs. Thuộc tính động

Nếu bạn không cần thêm các ràng buộc bổ sung vào truy vấn mối quan hệ Eloquent, bạn có thể truy cập mối quan hệ như thể nó là một thuộc tính. Ví dụ: tiếp tục sử dụng các mô hình mẫu

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

35 và

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 của chúng tôi, chúng tôi có thể truy cập tất cả các bài đăng của người dùng như vậy

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

38

Thuộc tính mối quan hệ động thực hiện "tải chậm", nghĩa là chúng sẽ chỉ tải dữ liệu mối quan hệ khi bạn thực sự truy cập chúng. Do đó, các nhà phát triển thường sử dụng tải háo hức để tải trước các mối quan hệ mà họ biết sẽ được truy cập sau khi tải mô hình. Eager loading giúp giảm đáng kể các truy vấn SQL phải được thực thi để tải các mối quan hệ của mô hình

Truy vấn sự tồn tại của mối quan hệ

Khi truy xuất các bản ghi mô hình, bạn có thể muốn giới hạn kết quả của mình dựa trên sự tồn tại của một mối quan hệ. Ví dụ: hãy tưởng tượng bạn muốn truy xuất tất cả các bài đăng trên blog có ít nhất một nhận xét. Để làm như vậy, bạn có thể chuyển tên của mối quan hệ cho các phương thức

composer require watson/aggregate
892 và
composer require watson/aggregate
893

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

39

Bạn cũng có thể chỉ định một toán tử và giá trị đếm để tùy chỉnh thêm truy vấn

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

60

Các câu lệnh

composer require watson/aggregate
892 lồng nhau có thể được xây dựng bằng cách sử dụng ký hiệu "dấu chấm". Ví dụ: bạn có thể truy xuất tất cả các bài đăng có ít nhất một nhận xét có ít nhất một hình ảnh

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

61

Nếu bạn cần nhiều quyền hơn nữa, bạn có thể sử dụng các phương pháp

composer require watson/aggregate
895 và
composer require watson/aggregate
896 để xác định các ràng buộc truy vấn bổ sung đối với các truy vấn
composer require watson/aggregate
892 của mình, chẳng hạn như kiểm tra nội dung của nhận xét

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

62

Cảnh báo
Eloquent hiện không hỗ trợ truy vấn sự tồn tại của mối quan hệ trên các cơ sở dữ liệu. Các mối quan hệ phải tồn tại trong cùng một cơ sở dữ liệu.

Truy vấn tồn tại mối quan hệ nội tuyến

Nếu bạn muốn truy vấn sự tồn tại của một mối quan hệ với một điều kiện duy nhất, đơn giản gắn liền với truy vấn mối quan hệ, bạn có thể thấy thuận tiện hơn khi sử dụng các phương thức

composer require watson/aggregate
898,
composer require watson/aggregate
899,

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

500 và

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

501. Ví dụ: chúng tôi có thể truy vấn tất cả các bài đăng có nhận xét chưa được phê duyệt

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

63

Tất nhiên, giống như các cuộc gọi đến phương thức

composer require watson/aggregate
402 của trình tạo truy vấn, bạn cũng có thể chỉ định một toán tử

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

64

Truy vấn mối quan hệ vắng mặt

Khi truy xuất các bản ghi mô hình, bạn có thể muốn giới hạn kết quả của mình dựa trên việc không có mối quan hệ. Ví dụ: hãy tưởng tượng bạn muốn truy xuất tất cả các bài đăng trên blog không có bất kỳ nhận xét nào. Để làm như vậy, bạn có thể chuyển tên của mối quan hệ cho các phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

503 và

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

504

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

65

Nếu bạn cần nhiều quyền hơn nữa, bạn có thể sử dụng các phương pháp

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

505 và

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

506 để thêm các ràng buộc truy vấn bổ sung vào các truy vấn

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

503 của mình, chẳng hạn như kiểm tra nội dung của một nhận xét

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

66

Bạn có thể sử dụng ký hiệu "dấu chấm" để thực hiện truy vấn đối với mối quan hệ lồng nhau. Ví dụ: truy vấn sau sẽ truy xuất tất cả các bài đăng không có nhận xét;

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

67

Truy vấn hình thái đối với các mối quan hệ

Để truy vấn sự tồn tại của các mối quan hệ "morph to", bạn có thể sử dụng các phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

508 và

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

509. Các phương thức này chấp nhận tên của mối quan hệ làm đối số đầu tiên của chúng. Tiếp theo, các phương thức chấp nhận tên của các mô hình liên quan mà bạn muốn đưa vào truy vấn. Cuối cùng, bạn có thể cung cấp một bao đóng để tùy chỉnh truy vấn mối quan hệ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

68

Đôi khi, bạn có thể cần thêm các ràng buộc truy vấn dựa trên "loại" của mô hình đa hình liên quan. Bao đóng được truyền cho phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

508 có thể nhận giá trị

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

511 làm đối số thứ hai của nó. Đối số này cho phép bạn kiểm tra "loại" truy vấn đang được tạo

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

69

Truy vấn tất cả các mô hình liên quan

Thay vì chuyển một mảng các mô hình đa hình có thể có, bạn có thể cung cấp

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

512 dưới dạng giá trị ký tự đại diện. Điều này sẽ hướng dẫn Laravel truy xuất tất cả các loại đa hình có thể có từ cơ sở dữ liệu. Laravel sẽ thực hiện một truy vấn bổ sung để thực hiện thao tác này

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

70

Tổng hợp các mô hình liên quan

Đếm các mô hình liên quan

Đôi khi bạn có thể muốn đếm số mô hình có liên quan cho một mối quan hệ nhất định mà không thực sự tải các mô hình. Để thực hiện điều này, bạn có thể sử dụng phương pháp

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

513. Phương pháp

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

513 sẽ đặt một thuộc tính

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

515 trên các mô hình kết quả

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

71

Bằng cách chuyển một mảng tới phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

513, bạn có thể thêm "số đếm" cho nhiều mối quan hệ cũng như thêm các ràng buộc bổ sung cho các truy vấn

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

72

Bạn cũng có thể đặt tên cho kết quả đếm mối quan hệ, cho phép nhiều lần đếm trên cùng một mối quan hệ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

73

Đang tải số lượng bị hoãn

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

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

517, bạn có thể tải số lượng mối quan hệ sau khi mô hình gốc đã được truy xuất

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

74

Nếu bạn cần đặt các ràng buộc truy vấn bổ sung cho truy vấn đếm, bạn có thể chuyển một mảng được khóa bởi các mối quan hệ mà bạn muốn đếm. Các giá trị mảng phải là các bao đóng nhận phiên bản trình tạo truy vấn

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

75

Đếm mối quan hệ & Tuyên bố chọn tùy chỉnh

Nếu bạn đang kết hợp

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

513 với câu lệnh

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

519, hãy đảm bảo rằng bạn gọi

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

513 sau phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

519

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

76

Các chức năng tổng hợp khác

Ngoài phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

513, Eloquent còn cung cấp các phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

523,

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

524,

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

525,

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

526 và

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

527. Các phương pháp này sẽ đặt thuộc tính

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

528 trên các mô hình kết quả của bạn

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

77

Nếu bạn muốn truy cập kết quả của hàm tổng hợp bằng tên khác, bạn có thể chỉ định bí danh của riêng mình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

78

Giống như phương pháp

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

517, các phiên bản hoãn lại của các phương pháp này cũng có sẵn. Các hoạt động tổng hợp bổ sung này có thể được thực hiện trên các mô hình Eloquent đã được truy xuất

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

79

Nếu bạn đang kết hợp các phương thức tổng hợp này với câu lệnh

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

519, hãy đảm bảo rằng bạn gọi các phương thức tổng hợp sau phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

519

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

76

Đếm các mô hình liên quan trên Morph đến các mối quan hệ

Nếu bạn muốn háo hức tải mối quan hệ "morph to", cũng như số lượng mô hình liên quan cho các thực thể khác nhau có thể được trả về bởi mối quan hệ đó, bạn có thể sử dụng phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

532 kết hợp với phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

534 của mối quan hệ
composer require watson/aggregate
821

Trong ví dụ này, giả sử rằng các mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

535 và

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 có thể tạo ra các mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

537. Chúng tôi sẽ giả sử mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

537 xác định mối quan hệ "morph to" có tên là

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

539 cho phép chúng tôi truy xuất mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

535 hoặc

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 gốc cho một phiên bản

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

537 đã cho. Ngoài ra, giả sử rằng các mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

535 "có nhiều" mô hình
composer require watson/aggregate
851 và mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 "có nhiều" mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

71

Bây giờ, hãy tưởng tượng chúng ta muốn truy xuất các phiên bản

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

537 và háo hức tải các mô hình chính của

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

539 cho mỗi phiên bản

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

537. Ngoài ra, chúng tôi muốn truy xuất số lượng thẻ được liên kết với mỗi ảnh của phụ huynh và số lượng nhận xét được liên kết với mỗi bài đăng của phụ huynh

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

31

Đang tải số lượng bị hoãn

Giả sử chúng tôi đã truy xuất một bộ mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

537 và bây giờ chúng tôi muốn tải số lượng mối quan hệ lồng nhau cho các mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

539 khác nhau được liên kết với nguồn cấp dữ liệu hoạt động. Bạn có thể sử dụng phương pháp

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

552 để thực hiện điều này

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

32

háo hức tải

Khi truy cập các mối quan hệ Eloquent dưới dạng thuộc tính, các mô hình liên quan được "tải chậm". Điều này có nghĩa là dữ liệu mối quan hệ không thực sự được tải cho đến khi bạn truy cập thuộc tính lần đầu tiên. Tuy nhiên, Eloquent có thể "eager load" các mối quan hệ tại thời điểm bạn truy vấn model gốc. Tải háo hức làm giảm bớt vấn đề truy vấn "N + 1". Để minh họa vấn đề truy vấn N + 1, hãy xem xét mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

553 "thuộc về" mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

554

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

33

Bây giờ, hãy truy xuất tất cả sách và tác giả của chúng

Vòng lặp này sẽ thực hiện một truy vấn để truy xuất tất cả sách trong bảng cơ sở dữ liệu, sau đó thực hiện một truy vấn khác cho từng cuốn sách để truy xuất tác giả của cuốn sách. Vì vậy, nếu chúng tôi có 25 cuốn sách, đoạn mã trên sẽ chạy 26 truy vấn. một cho cuốn sách gốc và 25 truy vấn bổ sung để truy xuất tác giả của mỗi cuốn sách

Rất may, chúng tôi có thể sử dụng tải háo hức để giảm thao tác này xuống chỉ còn hai truy vấn. Khi tạo truy vấn, bạn có thể chỉ định mối quan hệ nào sẽ được tải háo hức bằng phương pháp

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

532

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

34

Đối với thao tác này, chỉ có hai truy vấn sẽ được thực hiện - một truy vấn để truy xuất tất cả sách và một truy vấn để truy xuất tất cả tác giả của tất cả sách

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

35

Háo hức tải nhiều mối quan hệ

Đôi khi bạn có thể cần háo hức tải một số mối quan hệ khác nhau. Để làm như vậy, chỉ cần chuyển một mảng các mối quan hệ cho phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

532

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

36

Đang tải háo hức lồng nhau

Để háo hức tải các mối quan hệ của một mối quan hệ, bạn có thể sử dụng cú pháp "chấm". Ví dụ: hãy háo hức tải tất cả các tác giả của cuốn sách và tất cả các địa chỉ liên hệ cá nhân của tác giả

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

37

Ngoài ra, bạn có thể chỉ định các mối quan hệ được tải háo hức lồng nhau bằng cách cung cấp một mảng lồng nhau cho phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

532, điều này có thể thuận tiện khi háo hức tải nhiều mối quan hệ lồng nhau

Đang tải háo hức lồng nhau
composer require watson/aggregate
821 Mối quan hệ

Nếu bạn muốn háo hức tải một mối quan hệ

composer require watson/aggregate
821, cũng như các mối quan hệ lồng nhau trên các thực thể khác nhau có thể được trả về bởi mối quan hệ đó, bạn có thể sử dụng phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

532 kết hợp với phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

562 của mối quan hệ
composer require watson/aggregate
821. Để giúp minh họa phương pháp này, hãy xem xét mô hình sau

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

38

Trong ví dụ này, giả sử các mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

563,

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

535 và

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 có thể tạo ra các mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

537. Ngoài ra, giả sử rằng các mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

563 thuộc về một mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

568, các mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

535 được liên kết với các mô hình
composer require watson/aggregate
851 và các mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 thuộc về một mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

554

Sử dụng các định nghĩa và mối quan hệ mô hình này, chúng tôi có thể truy xuất các phiên bản mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

537 và tải háo hức tất cả các mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

539 và các mối quan hệ lồng nhau tương ứng của chúng

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

39

Háo hức tải các cột cụ thể

Bạn có thể không phải lúc nào cũng cần mọi cột từ các mối quan hệ mà bạn đang truy xuất. Vì lý do này, Eloquent cho phép bạn chỉ định cột nào của mối quan hệ mà bạn muốn truy xuất

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

00

Cảnh báo
Khi sử dụng tính năng này, bạn phải luôn bao gồm cột

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

47 và mọi cột khóa ngoại có liên quan trong danh sách các cột bạn muốn truy xuất.

Háo hức tải theo mặc định

Đôi khi bạn có thể muốn luôn tải một số mối quan hệ khi truy xuất một mô hình. Để thực hiện điều này, bạn có thể xác định thuộc tính

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

576 trên mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

01

Nếu bạn muốn xóa một mục khỏi thuộc tính

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

576 cho một truy vấn, bạn có thể sử dụng phương pháp

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

578

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

02

Nếu bạn muốn ghi đè tất cả các mục trong thuộc tính

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

576 cho một truy vấn, bạn có thể sử dụng phương pháp

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

580

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

03

Hạn chế tải háo hức

Đôi khi bạn có thể muốn tải một mối quan hệ háo hức nhưng cũng chỉ định các điều kiện truy vấn bổ sung cho truy vấn tải háo hức. Bạn có thể thực hiện việc này bằng cách chuyển một mảng các mối quan hệ tới phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

532 trong đó khóa mảng là tên mối quan hệ và giá trị mảng là một bao đóng có thêm các ràng buộc bổ sung cho truy vấn tải háo hức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

04

Trong ví dụ này, Eloquent sẽ chỉ háo hức tải các bài đăng trong đó cột

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

582 của bài đăng chứa từ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

583. Bạn có thể gọi các phương thức xây dựng truy vấn khác để tùy chỉnh thêm hoạt động tải háo hức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

05

Cảnh báo
Không được sử dụng các phương thức trình tạo truy vấn

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

584 và

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

585 khi hạn chế tải háo hức.

Hạn chế tải háo hức của các mối quan hệ
composer require watson/aggregate
821

Nếu bạn muốn tải một mối quan hệ

composer require watson/aggregate
821, Eloquent sẽ chạy nhiều truy vấn để tìm nạp từng loại mô hình có liên quan. Bạn có thể thêm các ràng buộc bổ sung cho từng truy vấn này bằng cách sử dụng phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

589 của quan hệ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

588

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

06

Trong ví dụ này, Eloquent sẽ chỉ háo hức tải các bài đăng chưa bị ẩn và video có giá trị "giáo dục"

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

590

Hạn chế tải háo hức với sự tồn tại của mối quan hệ

Đôi khi, bạn có thể thấy mình cần kiểm tra sự tồn tại của một mối quan hệ đồng thời tải mối quan hệ dựa trên cùng điều kiện. Ví dụ: bạn có thể chỉ muốn truy xuất các mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

35 có mô hình con

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 phù hợp với một điều kiện truy vấn nhất định trong khi cũng háo hức tải các bài đăng phù hợp. Bạn có thể thực hiện việc này bằng cách sử dụng phương pháp

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

593

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

07

Háo hức tải lười biếng

Đôi khi bạn có thể cần háo hức tải một mối quan hệ sau khi mô hình gốc đã được truy xuất. Ví dụ: điều này có thể hữu ích nếu bạn cần tự động quyết định có tải các mô hình liên quan hay không

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

08

Nếu bạn cần đặt các ràng buộc truy vấn bổ sung cho truy vấn tải háo hức, bạn có thể chuyển một mảng được khóa bởi các mối quan hệ mà bạn muốn tải. Các giá trị mảng phải là các phiên bản đóng nhận phiên bản truy vấn

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

09

Để chỉ tải một mối quan hệ khi nó chưa được tải, hãy sử dụng phương pháp

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

594

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

10

Đang tải háo hức lười biếng lồng nhau &
composer require watson/aggregate
821

Nếu bạn muốn háo hức tải một mối quan hệ

composer require watson/aggregate
821, cũng như các mối quan hệ lồng nhau trên các thực thể khác nhau có thể được trả về bởi mối quan hệ đó, bạn có thể sử dụng phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

597

Phương thức này chấp nhận tên của mối quan hệ

composer require watson/aggregate
821 làm đối số đầu tiên và một mảng các cặp mô hình/mối quan hệ làm đối số thứ hai. Để giúp minh họa phương pháp này, hãy xem xét mô hình sau

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

38

Trong ví dụ này, giả sử các mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

563,

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

535 và

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 có thể tạo ra các mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

537. Ngoài ra, giả sử rằng các mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

563 thuộc về một mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

568, các mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

535 được liên kết với các mô hình
composer require watson/aggregate
851 và các mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 thuộc về một mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

554

Sử dụng các định nghĩa và mối quan hệ mô hình này, chúng tôi có thể truy xuất các phiên bản mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

537 và tải háo hức tất cả các mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

539 và các mối quan hệ lồng nhau tương ứng của chúng

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

12

Ngăn chặn Lazy Loading

Như đã thảo luận trước đây, các mối quan hệ tải háo hức thường có thể mang lại lợi ích hiệu suất đáng kể cho ứng dụng của bạn. Do đó, nếu muốn, bạn có thể hướng dẫn Laravel luôn ngăn chặn quá trình lazy loading của các quan hệ. Để thực hiện điều này, bạn có thể gọi phương thức

$user->posts[]->where['active', 1]->get[];

111 được cung cấp bởi lớp mô hình Eloquent cơ sở. Thông thường, bạn nên gọi phương thức này trong phương thức
composer require watson/aggregate
876 của lớp

$user->posts[]->where['active', 1]->get[];

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

Phương thức

$user->posts[]->where['active', 1]->get[];

111 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

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

13

Sau khi ngăn chặn lazy loading, Eloquent sẽ đưa ra một ngoại lệ

$user->posts[]->where['active', 1]->get[];

115 khi ứng dụng của bạn cố gắng lazy load bất kỳ mối quan hệ Eloquent nào

Bạn có thể tùy chỉnh hành vi vi phạm tải chậm bằng phương pháp

$user->posts[]->where['active', 1]->get[];

116. Ví dụ: sử dụng phương pháp này, bạn có thể hướng dẫn các vi phạm tải chậm chỉ được ghi lại thay vì làm gián đoạn quá trình thực thi của ứng dụng với các ngoại lệ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

14

Chèn và cập nhật các mô hình liên quan

Phương pháp

$user->posts[]->where['active', 1]->get[];

117

Eloquent cung cấp các phương thức thuận tiện để thêm các mô hình mới vào các mối quan hệ. Ví dụ: có lẽ bạn cần thêm nhận xét mới vào bài đăng. Thay vì đặt thủ công thuộc tính

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

74 trên mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

71, bạn có thể chèn nhận xét bằng phương thức

$user->posts[]->where['active', 1]->get[];

117 của mối quan hệ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

15

Lưu ý rằng chúng tôi không truy cập mối quan hệ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

75 dưới dạng thuộc tính động. Thay vào đó, chúng tôi đã gọi phương thức

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

75 để lấy một thể hiện của mối quan hệ. Phương pháp

$user->posts[]->where['active', 1]->get[];

117 sẽ tự động thêm giá trị

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

74 thích hợp vào mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

71 mới

Nếu bạn cần lưu nhiều mô hình liên quan, bạn có thể sử dụng phương thức

$user->posts[]->where['active', 1]->get[];

126

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

16

Các phương thức

$user->posts[]->where['active', 1]->get[];

117 và

$user->posts[]->where['active', 1]->get[];

126 sẽ duy trì các phiên bản mô hình đã cho, nhưng sẽ không thêm các mô hình mới được duy trì vào bất kỳ mối quan hệ trong bộ nhớ nào đã được tải vào mô hình gốc. Nếu bạn định truy cập vào mối quan hệ sau khi sử dụng các phương pháp

$user->posts[]->where['active', 1]->get[];

117 hoặc

$user->posts[]->where['active', 1]->get[];

126, bạn có thể sử dụng phương pháp

$user->posts[]->where['active', 1]->get[];

131 để tải lại mô hình và các mối quan hệ của nó

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

17

Lưu đệ quy các mô hình & mối quan hệ

Nếu bạn muốn

$user->posts[]->where['active', 1]->get[];

117 mô hình của mình và tất cả các mối quan hệ liên quan của nó, bạn có thể sử dụng phương pháp

$user->posts[]->where['active', 1]->get[];

133. Trong ví dụ này, mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 sẽ được lưu cũng như các bình luận của nó và tác giả của bình luận

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

18

Phương pháp

$user->posts[]->where['active', 1]->get[];

135

Ngoài các phương thức

$user->posts[]->where['active', 1]->get[];

117 và

$user->posts[]->where['active', 1]->get[];

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

$user->posts[]->where['active', 1]->get[];

135, phương thức này chấp nhận một mảng các thuộc tính, tạo một mô hình và chèn nó vào cơ sở dữ liệu. Sự khác biệt giữa

$user->posts[]->where['active', 1]->get[];

117 và

$user->posts[]->where['active', 1]->get[];

135 là

$user->posts[]->where['active', 1]->get[];

117 chấp nhận một phiên bản mô hình Eloquent đầy đủ trong khi

$user->posts[]->where['active', 1]->get[];

135 chấp nhận một PHP

$user->posts[]->where['active', 1]->get[];

143 đơn giản. Mô hình mới được tạo sẽ được trả về bằng phương thức

$user->posts[]->where['active', 1]->get[];

135

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

19

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

$user->posts[]->where['active', 1]->get[];

145 để tạo nhiều mô hình liên quan

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

20

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

$user->posts[]->where['active', 1]->get[];

146,

$user->posts[]->where['active', 1]->get[];

147,

$user->posts[]->where['active', 1]->get[];

148 và

$user->posts[]->where['active', 1]->get[];

149 để tạo và cập nhật các mô hình về mối quan hệ

Lưu ý
Trước khi sử dụng phương pháp

$user->posts[]->where['active', 1]->get[];

135, hãy nhớ xem lại tài liệu chuyển nhượng hàng loạt.

Thuộc về các mối quan hệ

Nếu bạn muốn gán model con cho model gốc mới, bạn có thể sử dụng phương thức

$user->posts[]->where['active', 1]->get[];

151. Trong ví dụ này, mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

35 xác định mối quan hệ của

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

57 với mô hình

$user->posts[]->where['active', 1]->get[];

154. Phương thức

$user->posts[]->where['active', 1]->get[];

151 này sẽ đặt khóa ngoại trên mô hình con

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

21

Để xóa mô hình gốc khỏi mô hình con, bạn có thể sử dụng phương pháp

$user->posts[]->where['active', 1]->get[];

156. Phương pháp này sẽ đặt khóa ngoại của mối quan hệ thành

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

97

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

22

Nhiều đến nhiều mối quan hệ

Gắn / Tháo

Eloquent cũng cung cấp các phương thức giúp làm việc với mối quan hệ nhiều-nhiều thuận tiện hơn. Ví dụ: hãy tưởng tượng một người dùng có thể có nhiều vai trò và một vai trò có thể có nhiều người dùng. Bạn có thể sử dụng phương pháp

$user->posts[]->where['active', 1]->get[];

158 để gắn vai trò cho người dùng bằng cách chèn bản ghi vào bảng trung gian của mối quan hệ

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

23

Khi đính kèm một mối quan hệ vào một mô hình, bạn cũng có thể chuyển một mảng dữ liệu bổ sung để chèn vào bảng trung gian

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

24

Đôi khi có thể cần phải xóa vai trò khỏi người dùng. Để xóa bản ghi mối quan hệ nhiều-nhiều, hãy sử dụng phương pháp

$user->posts[]->where['active', 1]->get[];

159. Phương thức

$user->posts[]->where['active', 1]->get[];

159 sẽ xóa bản ghi thích hợp ra khỏi bảng trung gian;

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

25

Để thuận tiện,

$user->posts[]->where['active', 1]->get[];

158 và

$user->posts[]->where['active', 1]->get[];

159 cũng chấp nhận mảng ID làm đầu vào

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

26

Hiệp hội đồng bộ hóa

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

$user->posts[]->where['active', 1]->get[];

163 để xây dựng các liên kết nhiều-nhiều. Phương thức

$user->posts[]->where['active', 1]->get[];

163 chấp nhận một mảng ID để đặt trên bảng trung gian. Bất kỳ ID nào không có trong mảng đã cho sẽ bị xóa khỏi bảng trung gian. Vì vậy, sau khi thao tác này hoàn tất, chỉ các ID trong mảng đã cho sẽ tồn tại trong bảng trung gian

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

27

Bạn cũng có thể chuyển các giá trị bảng trung gian bổ sung bằng ID

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

28

Nếu bạn muốn chèn các giá trị bảng trung gian giống nhau với từng ID mô hình được đồng bộ hóa, bạn có thể sử dụng phương pháp

$user->posts[]->where['active', 1]->get[];

165

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

29

Nếu bạn không muốn tách các ID hiện có bị thiếu khỏi mảng đã cho, bạn có thể sử dụng phương thức

$user->posts[]->where['active', 1]->get[];

166

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

30

Chuyển đổi hiệp hội

Mối quan hệ nhiều-nhiều cũng cung cấp một phương thức

$user->posts[]->where['active', 1]->get[];

167 để "chuyển đổi" trạng thái tệp đính kèm của các ID mô hình có liên quan đã cho. Nếu ID đã cho hiện đang được đính kèm, nó sẽ bị tách ra. Tương tự như vậy, nếu nó hiện đang được tách ra, nó sẽ được gắn vào

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

31

Bạn cũng có thể chuyển các giá trị bảng trung gian bổ sung bằng ID

Cập nhật một bản ghi trên bảng trung gian

Nếu bạn cần cập nhật một hàng hiện có trong bảng trung gian của mối quan hệ của mình, bạn có thể sử dụng phương pháp

$user->posts[]->where['active', 1]->get[];

168. Phương thức này chấp nhận khóa ngoại của bản ghi trung gian và một mảng các thuộc tính để cập nhật

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

32

Chạm vào dấu thời gian của phụ huynh

Khi một mô hình xác định mối quan hệ của

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

57 hoặc
composer require watson/aggregate
454 với một mô hình khác, chẳng hạn như một

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

71 thuộc về một

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82, đôi khi sẽ rất hữu ích nếu cập nhật dấu thời gian của cha mẹ khi mô hình con được cập nhật

Ví dụ: khi mô hình

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

71 được cập nhật, bạn có thể muốn tự động "chạm" dấu thời gian
composer require watson/aggregate
476 của

use Illuminate\Database\Eloquent\Model;

* Get the phone associated with the user.

return $this->hasOne[Phone::class];

82 sở hữu để nó được đặt thành ngày và giờ hiện tại. Để thực hiện điều này, bạn có thể thêm thuộc tính

$user->posts[]->where['active', 1]->get[];

176 vào mô hình con của mình có chứa tên của các mối quan hệ sẽ được cập nhật dấu thời gian
composer require watson/aggregate
476 của chúng khi mô hình con được cập nhật

Tổng hợp trong Laravel là gì?

Một tập hợp là một lớp quyết định ghi lại các sự kiện dựa trên các sự kiện trong quá khứ .

Làm cách nào để tham gia hai bảng trong Laravel?

Tổng quan. Bạn có thể nối hai bảng trong một truy vấn khi cần dữ liệu từ hai bảng trở lên. .
Phương thức tham gia []. Phương thức join[] là một phần của trình tạo truy vấn và được sử dụng để thực hiện thao tác nối bảng trong Laravel
cú pháp. $người dùng = DB. bảng ['người dùng'].
Thông số. .
Ví dụ. .
Giải trình

Làm cách nào để cập nhật nhiều hàng trong Laravel hùng hồn?

Làm cách nào để cập nhật nhiều bản ghi trong Laravel? .
ví dụ 1. sử dụng Ứng dụng\Mô hình\Sản phẩm; . .
ví dụ 2. sử dụng Ứng dụng\Mô hình\Sản phẩm; . .
ví dụ 3. đọc thêm. Laravel Tạo bản ghi nếu không tồn tại Ví dụ. sử dụng Ứng dụng\Mô hình\Sản phẩm;

Làm cách nào để tham gia hai bảng trong Laravel 8?

Dưới đây, bạn có thể tìm thấy hướng dẫn từng bước về cách tham gia nhiều bảng bằng mô hình hùng hồn của Laravel. .
Tải xuống khung Laravel
Thực hiện kết nối cơ sở dữ liệu
Tạo lớp mô hình
Tạo lớp điều khiển
Tạo tập tin View Blade
Đặt tuyến đường
Chạy máy chủ Laravel

Chủ Đề