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ìnhuse 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ìnhcomposer require watson/aggregate4
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];
40composer require watson/aggregate8
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];
40use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
5Xá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[];
1Khi 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];
36Eloquent 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];
57use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
3Nế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 chause Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
6mộ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];
7Hã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];
74Khi 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 modeluse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
3Vì 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ấnuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
0Giố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];
78use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
1Mộ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];
57use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
2Khi 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];
81use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
3Trong 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];
71Eloquent 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];
74Tuy 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];
57use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
4Nế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ạnuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
5Mô 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];
82use 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/aggregate401
use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
7Truy 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/aggregate402 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];
8Tuy 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/aggregate403, 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];
9Bạ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/aggregate403. 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/aggregate40
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/aggregate41
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/aggregate407, 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/aggregate409
composer require watson/aggregate42
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/aggregate43
Theo mặc định, các phương thức
composer require watson/aggregate410 và
composer require watson/aggregate411 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/aggregate409, 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/aggregate409 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/aggregate414 hoặc
composer require watson/aggregate415] sẽ áp dụng khi truy vấn mô hình liên quan
composer require watson/aggregate44
Cảnh báo
Vì PostgreSQL không hỗ trợ thực thi hàmcomposer require watson/aggregate416 đố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/aggregate417 có thể có nhiều mô hình
composer require watson/aggregate418 đượ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/aggregate419
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/aggregate409 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/aggregate409. 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/aggregate45
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/aggregate422 có thể được liên kết với một kiểu máy
composer require watson/aggregate423 và mỗi kiểu máy
composer require watson/aggregate423 có thể được liên kết với một kiểu máy
composer require watson/aggregate425. 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/aggregate423. 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/aggregate422
composer require watson/aggregate46
Đố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 gianCô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 giancomposer require watson/aggregate47
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/aggregate430 có thể truy cập nhiều mô hình
composer require watson/aggregate431 thông qua mô hình trung gian
composer require watson/aggregate432. 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/aggregate430
composer require watson/aggregate48
Đối số đầu tiên được truyền cho phương thức
composer require watson/aggregate434 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/aggregate431 không chứa cột
composer require watson/aggregate436, nhưng mối quan hệ
composer require watson/aggregate434 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/aggregate438. Để truy xuất các mô hình này, Eloquent kiểm tra cột
composer require watson/aggregate436 trên bảng của mô hình trung gian
composer require watson/aggregate432. 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/aggregate431
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/aggregate434. Đố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/aggregate49
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ùngCấ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/aggregate445,
composer require watson/aggregate446 và
composer require watson/aggregate447. Bảng
composer require watson/aggregate447 đượ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/aggregate450. 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/aggregate446. Đ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/aggregate447 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/aggregate454. Phương thức
composer require watson/aggregate454 đượ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/aggregate446 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 quancomposer require watson/aggregate80
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/aggregate446
composer require watson/aggregate81
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/aggregate446 và tiếp tục xâu chuỗi các điều kiện vào truy vấn
composer require watson/aggregate82
Để 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/aggregate454
composer require watson/aggregate83
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/aggregate454. Đố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/aggregate84
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/aggregate454. Để 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/aggregate445 trên mô hình
composer require watson/aggregate465
composer require watson/aggregate85
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/aggregate454, 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/aggregate465 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/aggregate471 trên các mô hình
composer require watson/aggregate86
Lưu ý rằng mỗi mô hình
composer require watson/aggregate465 mà chúng tôi truy xuất được tự động gán một thuộc tính
composer require watson/aggregate471. 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/aggregate471. 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/aggregate87
Nếu bạn muốn bảng trung gian của mình có dấu thời gian
composer require watson/aggregate475 và
composer require watson/aggregate476 được Eloquent tự động duy trì, hãy gọi phương thức
composer require watson/aggregate477 khi xác định mối quan hệ
composer require watson/aggregate88
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 giancomposer require watson/aggregate475 vàcomposer require watson/aggregate476.
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/aggregate471. 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/aggregate482 thay vì
composer require watson/aggregate471. Đ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/aggregate484 khi xác định mối quan hệ
composer require watson/aggregate89
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];
50Lọ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/aggregate454 bằng cách sử dụng các phương pháp
composer require watson/aggregate486,
composer require watson/aggregate487,
composer require watson/aggregate488,
composer require watson/aggregate489,
composer require watson/aggregate490,
composer require watson/aggregate491 và
composer require watson/aggregate492 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/aggregate454 bằng phương pháp
composer require watson/aggregate494. 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];
52Xá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/aggregate495 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/aggregate496 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/aggregate497. Ví dụ: chúng tôi có thể xác định mô hình
composer require watson/aggregate465 sử dụng mô hình trục
composer require watson/aggregate499 tùy chỉnh
use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
53Khi định nghĩa mô hình
composer require watson/aggregate499, bạn nên mở rộng lớp
composer require watson/aggregate496
use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
54Cảnh báo
Mô hình xoay vòng không được sử dụng đặc điểmcomposer require watson/aggregate802. 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/aggregate803 được đặt thành
composer require watson/aggregate804
use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
55Mố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/aggregate807
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/aggregate810. 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/aggregate811 và
composer require watson/aggregate812 trên bảng
composer require watson/aggregate813. Cột
composer require watson/aggregate811 sẽ chứa giá trị ID của bài đăng hoặc người dùng, trong khi cột
composer require watson/aggregate812 sẽ chứa tên lớp của mô hình gốc. Cột
composer require watson/aggregate812 đượ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/aggregate817. Trong trường hợp này, cột sẽ chứa
composer require watson/aggregate818 hoặc
use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
99Cấ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];
56Truy 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/aggregate820
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/aggregate821. Trong trường hợp này, đó là phương pháp
composer require watson/aggregate817 trên mô hình
composer require watson/aggregate810. 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];
57Mối quan hệ
composer require watson/aggregate817 trên mô hình
composer require watson/aggregate810 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 ảnhCô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/aggregate821. 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/aggregate829 của PHP
use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
58Mộ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àyCấ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];
59Truy 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[];
10Bạ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/aggregate821. Trong trường hợp này, đó là phương pháp
composer require watson/aggregate833 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[];
11Mối quan hệ
composer require watson/aggregate833 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/aggregate807, 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/aggregate810, 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/aggregate409
$user->posts[]->where['active', 1]->get[];
12Tươ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[];
13Theo mặc định, các phương thức
composer require watson/aggregate410 và
composer require watson/aggregate411 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/aggregate409, 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/aggregate409 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/aggregate414 hoặc
composer require watson/aggregate415] sẽ áp dụng khi truy vấn mô hình liên quan
$user->posts[]->where['active', 1]->get[];
14Lư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/aggregate807 có thể chia sẻ mối quan hệ đa hình với mô hình
composer require watson/aggregate851. 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/aggregate807 sẽ chứa một phương thức
composer require watson/aggregate854 gọi phương thức
composer require watson/aggregate855 được cung cấp bởi lớp mô hình Eloquent cơ sở
Phương thức
composer require watson/aggregate855 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[];
15Xác định nghịch đảo của mối quan hệ
Tiếp theo, trên mô hình
composer require watson/aggregate851, 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/aggregate859. Cả hai phương thức này sẽ trả về kết quả của phương thức
composer require watson/aggregate860
Phương thức
composer require watson/aggregate860 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[];
16Truy 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/aggregate854
$user->posts[]->where['active', 1]->get[];
17Bạ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/aggregate860. 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/aggregate859 trên mô hình
composer require watson/aggregate851
$user->posts[]->where['active', 1]->get[];
18Cá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/aggregate807, thì mặc định
composer require watson/aggregate870 sẽ tương ứng là
composer require watson/aggregate818 hoặc
composer require watson/aggregate872. 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/aggregate874. 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[];
19Bạn có thể gọi phương thức
composer require watson/aggregate875 trong phương thức
composer require watson/aggregate876 của lớp
composer require watson/aggregate877 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/aggregate878 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/aggregate879
use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
30Cả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ộtcomposer require watson/aggregate880 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/aggregate881 để 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/aggregate881 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];
31Cả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ếtuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
32Bạ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];
34Ví dụ trên sẽ tạo SQL sau. Như bạn có thể thấy, mệnh đề
composer require watson/aggregate889 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];
35Trong 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];
36Ví 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];
37Phươ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ậyuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
38Thuộ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/aggregate892 và
composer require watson/aggregate893
use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
39Bạ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];
60Các câu lệnh
composer require watson/aggregate892 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];
61Nế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/aggregate895 và
composer require watson/aggregate896 để 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/aggregate892 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];
62Cả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/aggregate898,
composer require watson/aggregate899,
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ệtuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
63Tất nhiên, giống như các cuộc gọi đến phương thức
composer require watson/aggregate402 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];
64Truy 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];
504use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
65Nế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étuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
66Bạ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];
67Truy 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ạouse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
69Truy 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àyuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
70Tổ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];
71Bằ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ấnuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
72Bạ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ấtuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
74Nế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];
519use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
76Cá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ạnuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
77Nế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];
78Giố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ấtuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
79Nế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];
519use 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/aggregate821
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/aggregate851 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];
71Bâ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ụ huynhuse 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àyuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
32há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];
554use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
33Bâ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];
532use 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];
35Há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];
532use 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];
37Ngoà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/aggregate821, 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/aggregate821. Để 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];
38Trong 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/aggregate851 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];
554Sử 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únguse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
39Há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];
00Cảnh báo
Khi sử dụng tính năng này, bạn phải luôn bao gồm cột47 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.
use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
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ìnhuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
01Nế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];
578use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
02Nế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];
580use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
03Hạ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ứcuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
04Trong 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ứcuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
05Cảnh báo
Không được sử dụng các phương thức trình tạo truy vấn584 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.
use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
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/aggregate821, 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];
588use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
06Trong 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];
590Hạ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];
593use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
07Há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];
08Nế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];
594use 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/aggregate821, 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];
597Phương thức này chấp nhận tên của mối quan hệ
composer require watson/aggregate821 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];
38Trong 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/aggregate851 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];
554Sử 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únguse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
12Ngă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/aggregate876 của lớp
$user->posts[]->where['active', 1]->get[];
113 của ứng dụng của bạnPhươ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ấtuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
13Sau 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àoBạ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];
14Chè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
$user->posts[]->where['active', 1]->get[];
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];
15Lư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ớiNế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[];
126use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
16Cá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];
17Lư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ậnuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
18Phương pháp $user->posts[]->where['active', 1]->get[];
135
$user->posts[]->where['active', 1]->get[];
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[];
135use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
19Bạ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 quanuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
20Bạ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áp135, hãy nhớ xem lại tài liệu chuyển nhượng hàng loạt.
$user->posts[]->where['active', 1]->get[];
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 conuse 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];
97use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
22Nhiề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];
23Khi đí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àouse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
26Hiệ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 gianuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
27Bạ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];
28Nế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[];
165use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
29Nế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[];
166use Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
30Chuyể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àouse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
31Bạ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ậtuse Illuminate\Database\Eloquent\Model;
* Get the phone associated with the user.
return $this->hasOne[Phone::class];
32Chạ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/aggregate454 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ậtVí 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/aggregate476 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/aggregate476 của chúng khi mô hình con được cập nhật