* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
63 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
64 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
65 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
66 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
67 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
68 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
69 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
70 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
71 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
72 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ợ một số loại mối quan hệ khác nhau
Xác định mối quan hệ
Các mối quan hệ Eloquent được định nghĩa là các hàm trên các lớp mô hình Eloquent của bạn. Vì, giống như bản thân các mô hình Eloquent, các mối quan hệ cũng đóng vai trò là trình tạo truy vấn mạnh mẽ, việc xác định các mối quan hệ dưới dạng hàm cung cấp khả năng truy vấn và xâu chuỗi phương thức mạnh mẽ. Ví dụ
$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ột Đối Một
Mối quan hệ một đối một là một mối quan hệ rất cơ bản. Ví dụ: một mô hình
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
5 có thể được liên kết với một * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
6. Để xác định mối quan hệ này, chúng tôi đặt một phương thức * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
7 trên mô hình * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
5. Phương thức * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
7 sẽ trả về kết quả của phương thức * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
0 trên lớp mô hình Eloquent cơ sởuse Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
Đối số đầu tiên được truyền cho phương thức
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
0 là tên của 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 hàm quan hệ như thể chúng là các thuộc tính được xác định trên mô hình$phone = User::find[1]->phone;
Eloquent giả định khóa ngoại của mối quan hệ dựa trên tên mô hình. Trong trường hợp này, mô hình
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
6 tự động được coi là có khóa ngoại * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
3. 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 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
0 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
3Ngoài ra, Eloquent giả định rằng khóa ngoại phải có giá trị khớp với cột
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
5 của khóa gốc. Nói cách khác, Eloquent sẽ tìm kiếm giá trị của cột * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
5 của người dùng trong cột * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
3 của bản ghi * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
6. Nếu bạn muốn mối quan hệ sử dụng một giá trị khác với * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
5, bạn có thể chuyển đối số thứ ba cho phương thức * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
0 chỉ định khóa tùy chỉnh của bạn * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
0Xá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
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
6 từ * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
5 của chúng tôi. Bây giờ, hãy xác định mối quan hệ trên mô hình * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
6 sẽ cho phép chúng tôi truy cập vào * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
5 sở hữu điện thoại. Chúng ta có thể định nghĩa nghịch đảo của mối quan hệ * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
0 bằng cách sử dụng phương pháp * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
66 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
7Trong ví dụ trên, Eloquent sẽ cố gắng khớp
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
3 từ mô hình * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
6 với * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
5 trên mô hình * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
5. Eloquent xác định tên khóa ngoại mặc định bằng cách kiểm tra tên của phương thức quan hệ và thêm hậu tố tên phương thức là * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
21. Tuy nhiên, nếu khóa ngoại trên mô hình * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
6 không phải là * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
3, 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 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
66 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
Nếu mô hình gốc của bạn không sử dụng
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
5 làm khóa chính hoặc bạn muốn tham gia mô hình con vào một cột khác, bạn có thể chuyển đối số thứ ba cho phương thức * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
66 chỉ định khóa tùy chỉnh của bảng cha mẹ của bạn * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
một đến nhiều
Mối quan hệ "một-nhiều" được sử dụng để xác định các mối quan hệ trong đó một mô hình duy nhất sở hữu bất kỳ số lượng mô hình nào khác. 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 đặt một hàm trên mô hình Eloquent của bạn
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
6Hãy nhớ rằng, Eloquent sẽ tự động xác định cột khóa ngoại thích hợp trên mô hình
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
27. Theo quy ước, Eloquent sẽ lấy tên "trường hợp rắn" của mô hình sở hữu và thêm vào hậu tố là * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
21. Vì vậy, trong ví dụ này, Eloquent sẽ giả định khóa ngoại trên mô hình * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
27 là use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
00Khi mối 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 bằng cách truy cập thuộc tính
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
01. Hãy nhớ rằng, vì Eloquent cung cấp "thuộc tính động", nên chúng ta có thể truy cập các hàm quan hệ như thể chúng được định nghĩa là thuộc tính trên model * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
2Tất nhiên, vì tất cả các mối quan hệ cũng đóng vai trò là trình tạo truy vấn, bạn có thể thêm các ràng buộc khác mà nhận xét được truy xuất bằng cách gọi phương thức
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
01 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 record associated with the user.
return $this->hasOne['App\Phone'];
0Giống như phương thức
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
0, 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 record associated with the user.
return $this->hasOne['App\Phone'];
04use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
1Xác định nghịch đảo của mối quan hệ
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 record associated with the user.
return $this->hasOne['App\Phone'];
04, hãy xác định hàm quan hệ trên mô hình con gọi phương thức * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
66use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
2Khi mối quan hệ đã được xác định, chúng ta có thể truy xuất mô hình
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
07 cho một * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
27 bằng cách truy cập "thuộc tính động" của use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
09use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
3Trong ví dụ trên, Eloquent sẽ cố gắng khớp
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
00 từ mô hình * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
27 với * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
5 trên mô hình use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
07. Eloquent xác định tên khóa ngoại mặc định bằng cách kiểm tra tên của phương thức quan hệ và thêm hậu tố tên phương thức là * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
21. Tuy nhiên, nếu khóa ngoại trên mô hình * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
27 không phải là use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
00, 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 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
66use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
4Nếu mô hình gốc của bạn không sử dụng
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
5 làm khóa chính hoặc bạn muốn tham gia mô hình con vào một cột khác, bạn có thể chuyển đối số thứ ba cho phương thức * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
66 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 record associated with the user.
return $this->hasOne['App\Phone'];
5Nhiều nhiều
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ệ
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
0 và use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
04. Một ví dụ về mối quan hệ như vậy là một người dùng có nhiều vai trò, trong đó các vai trò này cũng được chia sẻ bởi những người dùng khác. Ví dụ: nhiều người dùng có thể có vai trò "Quản trị viên". Để xác định mối quan hệ này, cần có ba bảng cơ sở dữ liệu. use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
22, use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
23 và use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
24. Bảng use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
24 đượ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 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
3 và use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
27Mối quan hệ nhiều-nhiều được xác định bằng cách viết một phương thức gọi phương thức
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
28 trên lớp Eloquent cơ sở. Ví dụ: hãy xác định phương thức use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
23 trên mô hình * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
5 của chúng tôiuse Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
6Khi 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 động
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
23use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
7Tất nhiên, giống như tất cả các loại mối quan hệ khác, bạn có thể gọi phương thức
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
23 để tiếp tục xâu chuỗi các ràng buộc truy vấn vào mối quan hệuse Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
8Như đã đề cập trước đây, để xác định tên bảng của bảng nối của mối quan hệ, Eloquent sẽ nối hai tên model 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 truyền đối số thứ hai cho phương thức
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
28use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
9Ngoài việc tùy chỉnh tên của bảng tham gia, 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
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
28. Đố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$phone = User::find[1]->phone;
0Xá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 chỉ cần thực hiện một cuộc gọi khác tới
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
28 trên mô hình liên quan của mình. Để tiếp tục ví dụ về vai trò người dùng của chúng tôi, hãy xác định phương thức use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
22 trên mô hình use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
37$phone = User::find[1]->phone;
1Như bạn có thể thấy, mối quan hệ được xác định chính xác giống như đối tác
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
5 của nó, ngoại trừ việc chỉ tham chiếu mô hình use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
39. Vì chúng tôi đang sử dụng lại phương pháp use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
28, 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ềuTruy 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ử đối tượng
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
5 của chúng ta có nhiều đối tượng use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
37 có liên quan đến. Sau khi truy cập mối quan hệ này, chúng tôi có thể truy cập bảng trung gian bằng thuộc tính use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
43 trên các mô hình$phone = User::find[1]->phone;
2Lưu ý rằng mỗi mô hình
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
37 mà chúng tôi truy xuất được tự động gán một thuộc tính use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
43. Thuộc tính này chứa một model đại diện cho bảng trung gian và có thể được sử dụng giống như bất kỳ model Eloquent nào khácTheo mặc định, chỉ các khóa mô hình sẽ có mặt trên đối tượng
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
43. Nếu bảng tổng hợp 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ệ$phone = User::find[1]->phone;
3Nếu bạn muốn bảng tổng hợp của mình tự động duy trì dấu thời gian
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
47 và use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
48, hãy sử dụng phương pháp use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
49 trong định nghĩa mối quan hệ$phone = User::find[1]->phone;
4Có Nhiều Thông Qua
Mối quan hệ "có nhiều thông qua" cung cấp một lối tắt 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ụ: mô hình
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
50 có thể có nhiều mô hình use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
07 thông qua mô hình trung gian * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
5. Trong ví dụ này, bạn có thể dễ dàng thu thập tất cả các bài đăng trên blog cho một quốc gia nhất định. Hãy xem các bảng cần thiết để xác định mối quan hệ nàyMặc dù
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
53 không chứa cột use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
54, mối quan hệ use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
55 cung cấp quyền truy cập vào các bài đăng của một quốc gia thông qua use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
56. Để thực hiện truy vấn này, Eloquent kiểm tra use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
54 trên bảng trung gian use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
22. Sau khi tìm thấy ID người dùng phù hợp, chúng được sử dụng để truy vấn bảng use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
53Bây giờ chúng ta đã kiểm tra cấu trúc bảng cho mối quan hệ, hãy xác định nó trên mô hình
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
50$phone = User::find[1]->phone;
5Đối số đầu tiên được truyền cho phương thức
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
55 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á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 record associated with the user.
return $this->hasOne['App\Phone'];
55. Đối số thứ ba là tên của khóa ngoại trên mô hình trung gian, trong khi đối số thứ tư là tên của khóa ngoại trên mô hình cuối cùng$phone = User::find[1]->phone;
6quan hệ đa hình
Cấu trúc bảng
Mối quan hệ đa hình cho phép một mô hình thuộc về nhiều mô hình khác trên một liên kết đơn lẻ. Ví dụ: hãy tưởng tượng bạn muốn lưu trữ ảnh cho nhân viên và cho sản phẩm của mình. 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 record associated with the user.
return $this->hasOne['App\Phone'];
63 duy nhất cho cả hai trường hợp này. Đầ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àyHai cột quan trọng cần lưu ý là các cột
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
64 và use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
65 trên bảng use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
63. Cột use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
64 sẽ chứa giá trị ID của nhân viên hoặc sản phẩm sở hữu, trong khi cột use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
65 sẽ chứa tên lớp của mô hình sở hữu. Cột use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
65 là cách ORM xác định "loại" mô hình sở hữu nào sẽ trả về khi truy cập vào mối quan hệ use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
70Cấ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
$phone = User::find[1]->phone;
7Truy xuất quan hệ đa hình
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ả ảnh của một nhân viên, chúng ta chỉ cần sử dụng thuộc tính động
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
63$phone = User::find[1]->phone;
8Bạn cũng có thể truy xuất chủ sở hữu của một quan hệ đa hình từ 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
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
72. Trong trường hợp của chúng tôi, đó là phương pháp use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
70 trên mô hình use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
74. Vì vậy, chúng tôi sẽ truy cập phương thức đó dưới dạng thuộc tính động$phone = User::find[1]->phone;
9Mối quan hệ
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
70 trên mô hình use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
74 sẽ trả về một phiên bản use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
77 hoặc use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
78, tùy thuộc vào loại mô hình nào sở hữu ảnhCá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 mô hình liên quan. Chẳng hạn, đưa ra ví dụ ở trên, nơi một
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
79 có thể thuộc về một use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
07 hoặc một * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
27, thì mặc định là use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
82 sẽ là một trong hai use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
83 hoặc use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
84, tương ứng. Tuy nhiên, bạn có thể muốn tách cơ sở dữ liệu của mình khỏi cấu trúc bên trong của ứng dụng. Trong trường hợp đó, bạn có thể xác định một mối quan hệ "bản đồ hình thái" để hướng dẫn Eloquent sử dụng tên bảng được liên kết với từng mô hình thay vì tên lớpHoặc, bạn có thể chỉ định một chuỗi tùy chỉnh để liên kết với từng kiểu máy
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
30Bạn có thể đăng ký
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
85 trong use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
86 của mình hoặc tạo một nhà cung cấp dịch vụ riêng nếu muốnNhiều đến nhiều quan hệ đa hình
Cấu trúc bảng
Ngoài các quan hệ đa hình truyền thống, bạn cũng có thể định nghĩa các quan hệ đa hình "nhiều-nhiều". Ví dụ: mô hình blog
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
07 và use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
88 có thể chia sẻ mối quan hệ đa hình với mô hình use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
89. Sử dụng mối quan hệ đa hình nhiều-nhiều cho phép bạn có một danh sách các thẻ duy nhất được chia sẻ trên các bài đăng và video trên blog. Đầu tiên, hãy kiểm tra cấu trúc bảngCấ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 mô hình. Cả hai mô hình
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
07 và use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
88 sẽ có một phương thức use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
92 gọi phương thức use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
93 trên lớp Eloquent cơ sở * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
31Xác định nghịch đảo của mối quan hệ
Tiếp theo, trên mô hình
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
89, bạn nên xác định một phương thức cho từng mô hình liên quan 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 record associated with the user.
return $this->hasOne['App\Phone'];
53 và một phương thức use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
96 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
32Truy 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 chỉ cần sử dụng thuộc tính động
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
92 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
33Bạn cũng có thể truy xuất chủ sở hữu của một quan hệ đa hình từ 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
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
98. Trong trường hợp của chúng tôi, đó là các phương pháp use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
53 hoặc use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
96 trên mô hình use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
89. Vì vậy, bạn sẽ truy cập các phương thức đó dưới dạng thuộc tính động * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
34Quan hệ truy vấn
Vì tất cả các loại mối quan hệ Eloquent được xác định thông qua các hàm, nên bạn có thể gọi các hàm đó để lấy một thể hiện của mối quan hệ mà không thực sự thực hiện các truy vấn mối quan hệ. 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 đối với truy vấn mối quan hệ trước khi cuối cùng thực thi SQL đối với cơ sở dữ liệu của bạn
Ví dụ: hãy tưởng tượng một hệ thống blog trong đó mô hình
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
5 có nhiều mô hình use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
07 được liên kết * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
35Bạn có thể truy vấn mối quan hệ
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
53 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[];
Lưu ý rằng bạn có thể sử dụng bất kỳ phương pháp xây dựng truy vấn nào trên mối quan hệ
Phương thức quan hệ Vs. Thuộc tính động
Nếu bạn không cần thêm các ràng buộc bổ sung vào truy vấn mối quan hệ Eloquent, bạn có thể chỉ cần 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
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
5 và use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
07 của chúng tôi, chúng tôi có thể truy cập tất cả các bài đăng của người dùng như vậy * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
37Các thuộc tính động là "tải chậm", nghĩa là chúng sẽ chỉ tải dữ liệu mối quan hệ của chúng 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 cập các bản ghi cho một 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ệ sang phương thức
$phone = User::find[1]->phone;
07 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
38Bạn cũng có thể chỉ định một toán tử và đếm để tùy chỉnh thêm truy vấn
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
39Các câu lệnh
$phone = User::find[1]->phone;
07 lồng nhau cũng 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 bình luận và bình chọn * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
00Nếu bạn cần thêm sức mạnh, bạn có thể sử dụng các phương pháp
$phone = User::find[1]->phone;
09 và $phone = User::find[1]->phone;
10 để đặt điều kiện "ở đâu" cho các truy vấn $phone = User::find[1]->phone;
07 của mình. Các phương pháp này cho phép bạn thêm các ràng buộc tùy chỉnh vào ràng buộc mối quan hệ, chẳng hạn như kiểm tra nội dung của nhận xét * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
01háo hức tải
Khi truy cập các mối quan hệ Eloquent dưới dạng thuộc tính, dữ liệu mối quan hệ đượ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
$phone = User::find[1]->phone;
12 có liên quan đến $phone = User::find[1]->phone;
13 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
02Bâ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 1 truy vấn để truy xuất tất cả sách trên bảng, 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ả. Vì vậy, nếu chúng ta có 25 cuốn sách, vòng lặp này sẽ chạy 26 truy vấn. 1 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 2 truy vấn. Khi 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
$phone = User::find[1]->phone;
14 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
03Đối với hoạt động này, chỉ có hai truy vấn sẽ được thực hiện
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
04Há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 trong một thao tác. Để làm như vậy, chỉ cần chuyển các đối số bổ sung cho phương thức
$phone = User::find[1]->phone;
14 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
05Đang tải háo hức lồng nhau
Để háo hức tải các mối quan hệ lồng nhau, 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ả trong một Tuyên bố hùng hồn
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
06Hạ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 ràng buộc truy vấn bổ sung cho truy vấn tải háo hức. Đây là một ví dụ
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
07Trong ví dụ này, Eloquent sẽ chỉ háo hức tải các bài đăng nếu cột
$phone = User::find[1]->phone;
16 của bài đăng chứa từ $phone = User::find[1]->phone;
17. Tất nhiên, bạn có thể gọi trình tạo truy vấn khác để tùy chỉnh thêm hoạt động tải háo hức * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
08Há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
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
09Nế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
$phone = User::find[1]->phone;
18 cho phương thức $phone = User::find[1]->phone;
19 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
70Chèn các mô hình liên quan
Phương thức lưu
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 chèn một
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
27 mới cho kiểu máy use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
07. Thay vì đặt thủ công thuộc tính use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
00 trên * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
27, bạn có thể chèn trực tiếp * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
27 từ phương thức $phone = User::find[1]->phone;
25 của mối quan hệ * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
71Lưu ý rằng chúng tôi không truy cập mối quan hệ
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
01 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 record associated with the user.
return $this->hasOne['App\Phone'];
01 để lấy một thể hiện của mối quan hệ. Phương pháp $phone = User::find[1]->phone;
25 sẽ tự động thêm giá trị use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
00 thích hợp vào mô hình * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
27 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 pháp
$phone = User::find[1]->phone;
31 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
72Lưu & Nhiều đến Nhiều Mối quan hệ
Khi làm việc với mối quan hệ nhiều-nhiều, phương thức
$phone = User::find[1]->phone;
25 chấp nhận một mảng các thuộc tính bảng trung gian bổ sung làm đối số thứ hai của nó * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
73Phương thức tạo
Ngoài các phương thức
$phone = User::find[1]->phone;
25 và $phone = User::find[1]->phone;
31, bạn cũng có thể sử dụng phương thức $phone = User::find[1]->phone;
35, 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. Một lần nữa, sự khác biệt giữa $phone = User::find[1]->phone;
25 và $phone = User::find[1]->phone;
35 là $phone = User::find[1]->phone;
25 chấp nhận một phiên bản mô hình Eloquent đầy đủ trong khi $phone = User::find[1]->phone;
35 chấp nhận một PHP $phone = User::find[1]->phone;
40 đơn giản * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
74Trước khi sử dụng phương pháp
$phone = User::find[1]->phone;
35, hãy nhớ xem lại tài liệu về gán khối lượng thuộc tínhCập nhật các mối quan hệ "Thuộc về"
Khi cập nhật mối quan hệ
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
66, bạn có thể sử dụng phương pháp $phone = User::find[1]->phone;
43. Phương pháp này sẽ đặt khóa ngoại trên mô hình con * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
75Khi xóa mối quan hệ
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
66, bạn có thể sử dụng phương pháp $phone = User::find[1]->phone;
45. Phương thức này sẽ thiết lập lại khóa ngoại cũng như quan hệ trên mô hình con * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
76Nhiều đến nhiều mối quan hệ
Gắn / Tháo
Khi làm việc với các mối quan hệ nhiều-nhiều, Eloquent cung cấp một vài phương thức trợ giúp bổ sung để làm việc với các mô hình liên quan 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. Để gắn vai trò cho người dùng bằng cách chèn bản ghi vào bảng trung gian tham gia các mô hình, hãy sử dụng phương thức
$phone = User::find[1]->phone;
46 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
77Khi đí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
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
78Tất nhiên, đô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
$phone = User::find[1]->phone;
47. Phương thức $phone = User::find[1]->phone;
47 sẽ xóa bản ghi thích hợp ra khỏi bảng trung gian; * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
79Để thuận tiện,
$phone = User::find[1]->phone;
46 và $phone = User::find[1]->phone;
47 cũng chấp nhận mảng ID làm đầu vào * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
0Đồng bộ hóa để thuận tiện
Bạn cũng có thể sử dụng phương pháp
$phone = User::find[1]->phone;
51 để xây dựng các liên kết nhiều-nhiều. Phương thức $phone = User::find[1]->phone;
51 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 sẽ tồn tại trong bảng trung gian * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
1Bạn cũng có thể chuyển các giá trị bảng trung gian bổ sung bằng ID
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
2Chạm vào dấu thời gian của phụ huynh
Khi một mô hình
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
66 hoặc use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
28 một mô hình khác, chẳng hạn như một * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
27 thuộc về một use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
07, đôi khi sẽ rất hữu ích khi cập nhật dấu thời gian của mô hình gốc khi mô hình con được cập nhật. Ví dụ: khi mô hình * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
27 được cập nhật, bạn có thể muốn tự động "chạm" dấu thời gian use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
48 của use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
07 đang sở hữu. Hùng biện làm cho nó dễ dàng. Chỉ cần thêm thuộc tính $phone = User::find[1]->phone;
60 chứa tên của các mối quan hệ vào mô hình con * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
3Bây giờ, khi bạn cập nhật một
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key'];
27, thì use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
07 sở hữu cũng sẽ cập nhật cột use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
48 của nó