Tôi có một đối tượng với một số mối quan hệ và tôi cần kiểm tra xem các mối quan hệ này có trống hay không, tôi đang cố kiểm tra với is_null, isset,. = không xác định, v.v nhưng không có gì hoạt động, đây là mối quan hệ tôi nhận được khi nó trống
object[Illuminate\Database\Eloquent\Collection]#197 [1] {
["items":protected]=>
array[0] {
}
}
Có cách nào để kiểm tra dễ dàng không?
Cảm ơn.
Giải pháp tốt nhất
Có nhiều cách khác nhau để làm điều này
Trong chính truy vấn, bạn có thể lọc các mô hình không có bất kỳ mục liên quan nào
Model::has['relation']->get[]
Khi bạn có một mô hình, nếu bạn đã tải bộ sưu tập, bạn có thể kiểm tra số lượng bộ sưu tập
Hôm nay mình sẽ hướng dẫn các bạn ví dụ kiểm tra quan hệ rỗng trong laravel. Ví dụ này sẽ giúp bạn kiểm tra laravel nếu mối quan hệ tồn tại. Mình xin hướng dẫn các bạn cách kiểm tra dữ liệu quan hệ rỗng trong laravel. Tôi đã giải thích đơn giản từng bước kiểm tra xem mối quan hệ hùng hồn có trống không laravel
Bạn có thể sử dụng ví dụ này với các phiên bản laravel 6, laravel 7, laravel 8 và laravel 9
Nếu bạn muốn kiểm tra xem dữ liệu quan hệ của bạn có trống hay không trong laravel thì tôi sẽ giúp bạn một số cách để kiểm tra xem quan hệ của bạn có trống hay không trong laravel. bạn có thể xem các cách sau để kiểm tra xem một quan hệ có trống không trong laravel
Đầu tiên, tôi sẽ tạo mô hình Bài đăng mẫu với mối quan hệ nhận xét. Vì vậy, hãy xem mã mô hình bài đăng sau đây
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', 'other_key'];
1 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', 'other_key'];
2. Để 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', 'other_key'];
3 trên mô hình * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
1. Phương thức * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
3 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'];
6 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'];
6 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ìnhModel::has['relation']->get[]
1Eloquent 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', 'other_key'];
2 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'];
9. 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'];
6Model::has['relation']->get[]
5Ngoài ra, Eloquent giả định rằng khóa ngoại phải có giá trị khớp với cột
Model::has['relation']->get[]
61 [hoặc cột Model::has['relation']->get[]
62 tùy 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 Model::has['relation']->get[]
61 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'];
9 của bản ghi * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
2. Nếu bạn muốn mối quan hệ sử dụng một giá trị khác với Model::has['relation']->get[]
61, 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'];
6 chỉ định khóa tùy chỉnh của bạn$user->posts[]->where['active', 1]->get[];
3Xá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', 'other_key'];
2 từ * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
1 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', 'other_key'];
2 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', 'other_key'];
1 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'];
6 bằng cách sử dụng phương pháp $user->posts[]->where['active', 1]->get[];
93 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
0Trong ví dụ trên, Eloquent sẽ cố gắng so khớp
* Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
9 từ mô hình * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
2 với một Model::has['relation']->get[]
61 trên mô hình * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
1. 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à $user->posts[]->where['active', 1]->get[];
98. 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', 'other_key'];
2 không phải là * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
9, 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 $user->posts[]->where['active', 1]->get[];
93 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
9Nếu mô hình gốc của bạn không sử dụng
Model::has['relation']->get[]
61 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 $user->posts[]->where['active', 1]->get[];
93 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
Model::has['relation']->get[]
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
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
04. Theo quy ước, Eloquent sẽ lấy tên "snake case" của mô hình sở hữu và thêm vào hậu tố là $user->posts[]->where['active', 1]->get[];
98. Vì vậy, trong ví dụ này, Eloquent sẽ giả định khóa ngoại trên mô hình use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
04 là use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
07Khi 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'];
08. 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$user->posts[]->where['active', 1]->get[];
9Tấ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'];
08 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'];
6, 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'];
11use 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'];
11, hãy xác định hàm quan hệ trên mô hình con gọi phương thức $user->posts[]->where['active', 1]->get[];
93use 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'];
14 cho một use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
04 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'];
16use 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 so khớp
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
07 từ mô hình use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
04 với một Model::has['relation']->get[]
61 trên mô hình use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
14. 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à $user->posts[]->where['active', 1]->get[];
98. Tuy nhiên, nếu khóa ngoại trên mô hình use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
04 không phải là use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
07, 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 $user->posts[]->where['active', 1]->get[];
93use 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
Model::has['relation']->get[]
61 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 $user->posts[]->where['active', 1]->get[];
93 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'];
6 và use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
11. 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'];
29, use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
30 và use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
31. Bảng use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
31 đượ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'];
9 và use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
34Mố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'];
35 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'];
30 trên mô hình * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
1 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'];
30use 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'];
30 để 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 chuyể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'];
35use 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'];
35. Đố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 giaModel::has['relation']->get[]
10Xá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'];
35 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'];
29 trên mô hình use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
44Model::has['relation']->get[]
11Như 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', 'other_key'];
1 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'];
46. Vì chúng tôi đang sử dụng lại phương thức use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
35, 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', 'other_key'];
1 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'];
44 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'];
50 trên các mô hìnhModel::has['relation']->get[]
12Lư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'];
44 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'];
50. 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 phím 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'];
50. 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ệModel::has['relation']->get[]
13Nế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'];
54 và use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
55, 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'];
56 trong định nghĩa mối quan hệModel::has['relation']->get[]
14Lọc các mối quan hệ qua các cột của bảng trung gian
Bạn cũng có thể lọc các kết quả trả về bởi
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
35 bằng cách sử dụng các phương thức use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
58 và use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
59 khi xác định mối quan hệModel::has['relation']->get[]
15Có 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'];
60 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'];
14 thông qua mô hình trung gian * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
1. 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'];
63 không chứa cột use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
64, mối quan hệ use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
65 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'];
66. Để 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'];
64 trên bảng trung gian use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
29. 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'];
63Bâ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'];
60Model::has['relation']->get[]
16Đố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'];
65 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'];
65. Đố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 và đối số thứ năm là khóa cục bộModel::has['relation']->get[]
17quan 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 người dùng ứng dụng của bạn có thể "thích" cả bài đăng và nhận xét. 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'];
73 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'];
74 và use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
75 trên bảng use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
73. Cột use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
74 sẽ chứa giá trị ID của bài đăng hoặc nhận xét, trong khi cột use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
75 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'];
75 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'];
80Cấ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
Model::has['relation']->get[]
18Truy 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ả các lượt thích cho một bài đăng, 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'];
73Model::has['relation']->get[]
19Bạ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 cuộc gọi tới
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
82. 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'];
80 trên mô hình use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
84. Vì vậy, chúng tôi sẽ truy cập phương thức đó dưới dạng thuộc tính độngModel::has['relation']->get[]
50Mối quan hệ
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
80 trên mô hình use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
84 sẽ trả về một thực thể use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
14 hoặc use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
04, tùy thuộc vào loại mô hình nào sở hữu đối tượng tương tựCác loại đa hình tùy chỉnh
Theo mặc định, Laravel sẽ sử dụng tên lớp đủ điều kiện để lưu trữ loại 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'];
84 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'];
14 hoặc một use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
04, thì mặc định là use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
92 sẽ là một trong hai use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
93 hoặc use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
94, 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ớpModel::has['relation']->get[]
51Hoặ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
Model::has['relation']->get[]
52Bạn có thể đăng ký
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
95 trong chức năng use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
96 của use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
97 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'];
14 và use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
99 có thể chia sẻ mối quan hệ đa hình với mô hình Model::has['relation']->get[]
100. 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'];
14 và use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
99 sẽ có một phương thức Model::has['relation']->get[]
103 gọi phương thức Model::has['relation']->get[]
104 trên lớp Eloquent cơ sởModel::has['relation']->get[]
53Xác định nghịch đảo của mối quan hệ
Tiếp theo, trên mô hình
Model::has['relation']->get[]
100, 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'];
63 và một phương thức Model::has['relation']->get[]
107Model::has['relation']->get[]
54Truy 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
Model::has['relation']->get[]
103Model::has['relation']->get[]
55Bạ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
Model::has['relation']->get[]
109. 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'];
63 hoặc Model::has['relation']->get[]
107 trên mô hình Model::has['relation']->get[]
100. Vì vậy, bạn sẽ truy cập các phương thức đó dưới dạng thuộc tính độngModel::has['relation']->get[]
56Quan 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', 'other_key'];
1 có nhiều mô hình use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
14 được liên kếtModel::has['relation']->get[]
57Bạ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'];
63 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', 'other_key'];
1 và use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
14 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ậyModel::has['relation']->get[]
59Cá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
Model::has['relation']->get[]
118$user->posts[]->where['active', 1]->get[];
30Bạn cũng có thể chỉ định một toán tử và đếm để tùy chỉnh thêm truy vấn
$user->posts[]->where['active', 1]->get[];
31Các câu lệnh
Model::has['relation']->get[]
118 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$user->posts[]->where['active', 1]->get[];
32Nếu bạn cần thêm sức mạnh, bạn có thể sử dụng các phương pháp
Model::has['relation']->get[]
120 và Model::has['relation']->get[]
121 để đặt điều kiện "ở đâu" cho các truy vấn Model::has['relation']->get[]
118 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$user->posts[]->where['active', 1]->get[];
33Đếm kết quả mối quan hệ
Nếu bạn muốn đếm số lượng kết quả từ một mối quan hệ mà không thực sự tải chúng, bạn có thể sử dụng phương pháp
Model::has['relation']->get[]
123, phương pháp này sẽ đặt một cột Model::has['relation']->get[]
124 trên các mô hình kết quả của bạn. Ví dụ$user->posts[]->where['active', 1]->get[];
34Bạn có thể truy xuất "số lượng" cho nhiều mối quan hệ cũng như thêm các ràng buộc vào các truy vấn
$user->posts[]->where['active', 1]->get[];
35há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
Model::has['relation']->get[]
125 có liên quan đến Model::has['relation']->get[]
126$user->posts[]->where['active', 1]->get[];
36Bâ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
Model::has['relation']->get[]
127$user->posts[]->where['active', 1]->get[];
37Đối với hoạt động này, chỉ có hai truy vấn sẽ được thực hiện
$user->posts[]->where['active', 1]->get[];
38Há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
Model::has['relation']->get[]
127$user->posts[]->where['active', 1]->get[];
39Đ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', 'other_key'];
00Hạ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', 'other_key'];
01Trong ví dụ này, Eloquent sẽ chỉ háo hức tải các bài đăng trong đó cột
Model::has['relation']->get[]
129 của bài đăng chứa từ Model::has['relation']->get[]
130. Tất nhiên, bạn có thể gọi các phương thức xây dựng truy vấn khác để tùy chỉnh thêm hoạt động tải háo hức * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
02Há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', 'other_key'];
03Nế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
Model::has['relation']->get[]
131 cho phương thức Model::has['relation']->get[]
132 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
04Chè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
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
04 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'];
14. 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'];
07 trên use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
04, bạn có thể chèn trực tiếp use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
04 từ phương thức Model::has['relation']->get[]
138 của mối quan hệ * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
05Lư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'];
08 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'];
08 để lấy một thể hiện của mối quan hệ. Phương pháp Model::has['relation']->get[]
138 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'];
07 thích hợp vào mẫu use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
04 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
Model::has['relation']->get[]
144 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
06Lư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
Model::has['relation']->get[]
138 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', 'other_key'];
07Phương thức tạo
Ngoài các phương thức
Model::has['relation']->get[]
138 và Model::has['relation']->get[]
144, bạn cũng có thể sử dụng phương thức Model::has['relation']->get[]
148, 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 Model::has['relation']->get[]
138 và Model::has['relation']->get[]
148 là Model::has['relation']->get[]
138 chấp nhận một phiên bản mô hình Eloquent đầy đủ trong khi Model::has['relation']->get[]
148 chấp nhận một PHP Model::has['relation']->get[]
153 đơn giản * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
08Trước khi sử dụng phương pháp
Model::has['relation']->get[]
148, 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ệ
$user->posts[]->where['active', 1]->get[];
93, bạn có thể sử dụng phương pháp Model::has['relation']->get[]
156. 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', 'other_key'];
09Khi xóa mối quan hệ
$user->posts[]->where['active', 1]->get[];
93, bạn có thể sử dụng phương pháp Model::has['relation']->get[]
158. 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', 'other_key'];
90Nhiề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
Model::has['relation']->get[]
159 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
91Khi đí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', 'other_key'];
92Tấ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
Model::has['relation']->get[]
160. Phương thức Model::has['relation']->get[]
160 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', 'other_key'];
93Để thuận tiện,
Model::has['relation']->get[]
159 và Model::has['relation']->get[]
160 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', 'other_key'];
94Cập nhật bản ghi trên bảng Pivot
Nếu bạn cần cập nhật một hàng hiện có trong bảng tổng hợp của mình, bạn có thể sử dụng phương pháp
Model::has['relation']->get[]
164 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
95Hiệp hội đồng bộ hóa
Bạn cũng có thể sử dụng phương pháp
Model::has['relation']->get[]
165 để xây dựng các liên kết nhiều-nhiều. Phương thức Model::has['relation']->get[]
165 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', 'other_key'];
96Bạ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', 'other_key'];
97Nếu bạn không muốn tách các ID hiện có, bạn có thể sử dụng phương pháp
Model::has['relation']->get[]
167 * Get the user that owns the phone.
return $this->belongsTo['App\User', 'foreign_key', 'other_key'];
98Chạm vào dấu thời gian của phụ huynh
Khi một mô hình
$user->posts[]->where['active', 1]->get[];
93 hoặc use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
35 một mô hình khác, chẳng hạn như một use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
04 thuộc về một use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
14, đôi khi sẽ hữu ích nếu 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 use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
04 đượ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'];
55 của use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
14 đang sở hữu. Hùng biện làm cho nó dễ dàng. Chỉ cần thêm thuộc tính Model::has['relation']->get[]
175 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', 'other_key'];
99Bây giờ, khi bạn cập nhật một
use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
04, thì use Illuminate\Database\Eloquent\Model;
* Get the phone record associated with the user.
return $this->hasOne['App\Phone'];
14 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'];
55 của nó