Các mối quan hệ Eloquent của Laravel đượ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 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ẽ. Các mối quan hệ của Laravel có thể hơi khó hiểu. Nếu bạn chưa hiểu đầy đủ về cách các mối quan hệ trong Laravel hoạt động tại thời điểm này. Đừng lo lắng, Trong blog này, tôi đã thu thập một số mẹo và thủ thuật hữu ích về các mối quan hệ của mô hình laravel có thể hỗ trợ bạn nâng cấp quy trình phát triển web Laravel của mình
Phương pháp xây dựng truy vấn mới của // BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
2 Eloquent
// BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
Laravel
// BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
3 xuất xưởng với một phương pháp xây dựng truy vấn // BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
2 Eloquent mới. Điều này cho phép bạn xóa tên khóa ngoại BelongsTo khỏi các truy vấn của mình và thay vào đó, sử dụng phương thức quan hệ làm nguồn thông tin xác thực duy nhất// From:
$query->where['author_id', $author->id]
// To:
$query->whereBelongsTo[$author]
// Easily add more advanced filtering:
Post::query[]
->whereBelongsTo[$author]
->whereBelongsTo[$cateogry]
->whereBelongsTo[$section]
->get[];
// Specify a custom relationship:
$query->whereBelongsTo[$author, 'author']
Phương pháp // BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
5 của các mối quan hệ một đối một để so sánh các mô hình
// BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
Bây giờ chúng ta có thể so sánh giữa các mô hình liên quan mà không cần truy cập thêm cơ sở dữ liệu
// BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
Sử dụng hasMany để tạo Nhiều
Nếu bạn có mối quan hệ
// BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
6, bạn có thể sử dụng // BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
7 để lưu nhiều mục nhập "con" từ đối tượng "cha mẹ" của mình, tất cả trong một câuĐang tải háo hức đa cấp
Trong Laravel, bạn có thể Eager Load nhiều cấp trong một câu lệnh, trong ví dụ này, chúng tôi không chỉ tải quan hệ tác giả mà còn quan hệ quốc gia trên mô hình tác giả
$users = App\Book::with['author.country']->get[];
Háo hức tải với các cột chính xác
Bạn có thể thực hiện Laravel Eager Loading và chỉ định các cột chính xác mà bạn muốn nhận từ mối quan hệ
// BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
0Bạn có thể làm điều đó ngay cả trong các mối quan hệ cấp hai, sâu sắc hơn
// BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
1Truy vấn bộ lọc bổ sung về mối quan hệ
Nếu bạn muốn tải dữ liệu quan hệ, bạn có thể chỉ định một số giới hạn hoặc đơn đặt hàng trong hàm đóng. Ví dụ: nếu bạn muốn nhận các Quốc gia chỉ có ba thành phố lớn nhất của họ, thì đây là mã
// BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
2Thay vì thuộc về, hãy sử dụng hasMany
Đối với mối quan hệ thuộc về, thay vì chuyển ID của cha mẹ khi tạo bản ghi con, hãy sử dụng mối quan hệ
// BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
8 để tạo câu ngắn hơn// BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
4Kết hợp hai "whereHas"
Trong Eloquent, bạn có thể kết hợp
// BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
9 và $post = Post::find[1];
$post->comments[]->saveMany[[
new Comment[['message' => 'First comment']],
new Comment[['message' => 'Second comment']],
]];
0 trong một câu// BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
7Kiểm tra xem Phương thức quan hệ có tồn tại không
Nếu tên mối quan hệ Eloquent của bạn là động và bạn cần kiểm tra xem mối quan hệ có tên như vậy có tồn tại trên đối tượng hay không, hãy sử dụng hàm PHP
$post = Post::find[1];
$post->comments[]->saveMany[[
new Comment[['message' => 'First comment']],
new Comment[['message' => 'Second comment']],
]];
1// BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
9Một cách ngắn hơn để viết whereHas
Phát hành trong Laravel
$post = Post::find[1];
$post->comments[]->saveMany[[
new Comment[['message' => 'First comment']],
new Comment[['message' => 'Second comment']],
]];
2. một cách ngắn hơn để viết // BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
9 với một điều kiện đơn giản bên trong// BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
0Bạn có thể thêm điều kiện cho các mối quan hệ của mình
// BEFORE: the foreign key is taken from the Post model
$post->author_id === $user->id;
// BEFORE: An additional request is made to get the User model from the Author relationship
$post->author->is[$user];
// AFTER
$post->author[]->is[$user];
1Ghi chú
Tôi hy vọng rằng bằng cách làm theo các mẹo và thủ thuật này, bạn có thể cải thiện chất lượng và hiệu suất mã của mình cũng như cải thiện hành trình viết mã của mình. Nếu bạn là một doanh nghiệp và muốn tận dụng Laravel để phát triển ứng dụng web dựa trên PHP tiếp theo cho các yêu cầu tùy chỉnh của mình, bạn phải khám phá một nhóm tinh tế có trình độ về khuôn khổ Laravel. Vì vậy, Codebrisk sẵn sàng trợ giúp bạn với các yêu cầu phù hợp của bạn về Phát triển Laravel
Có một ý tưởng tuyệt vời? . com hoặc liên hệ với chúng tôi, nhân viên kinh doanh của chúng tôi sẽ liên hệ lại với bạn