Laravel lấy dữ liệu với mối quan hệ

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

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

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

________số 8

Đ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];
0

Bạ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];
1

Truy 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];
2

Thay 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];
4

Kế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];
7

Kiể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];
9

Mộ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];
0

Bạ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];
1

Ghi 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

Làm cách nào để lấy dữ liệu quan hệ trong bộ điều khiển Laravel?

Giả sử mối quan hệ giữa mô hình Sản phẩm và mô hình Sku của bạn là. .
Product public function skus[] { return hasMany['App/Sku','products_id']; .
Bộ điều khiển $products = Sản phẩm. with['skus']->get[];.
View foreach [$products as $product] { //$product->skus là tập hợp các mẫu Sku dd[ $product->skus ];

Làm cách nào để tìm nạp dữ liệu trong mối quan hệ một đến nhiều trong Laravel?

Truy xuất từ ​​một đến nhiều dữ liệu về mối quan hệ . tìm[1]; . Tương tự, chúng ta có thể truy xuất mô hình liên quan nghịch đảo. $brand = Brand::find[1]; $products = $brand->products; When we try to get products for a brand, Laravel will look into products table with the brand ID in the brand_id columns and grab all the products matching with this ID. Similarly, we can retrieve the inverse related model.

Làm cách nào để xác định mối quan hệ trong Laravel?

Để xác định mối quan hệ, trước tiên chúng ta cần xác định phương thức post[] trong mô hình Người dùng . Trong phương thức post[], chúng ta cần triển khai phương thức hasOne[] trả về kết quả. Hãy hiểu mối quan hệ 1-1 thông qua một ví dụ. Đầu tiên, chúng tôi thêm cột mới [user_id] vào bảng hiện có có tên là bài viết.

Làm cách nào để lấy dữ liệu từ cơ sở dữ liệu bằng Laravel?

Để lấy dữ liệu từ cơ sở dữ liệu MySQL bằng laravel framework trước tiên chúng ta phải tạo một bảng trong cơ sở dữ liệu. .
StudViewController. php [ứng dụng/Http/Bộ điều khiển/StudViewController. php]
stud_view. lưỡi. php [tài nguyên/lượt xem/stud_view. lưỡi. php]
trang web. php [tuyến/web. php]

Chủ Đề