Các bảng Pivot có thể gây nhầm lẫn và hơi khó hiểu lúc đầu. Trong bài viết nhanh này, chúng ta sẽ đi sâu vào tìm hiểu bảng tổng hợp là gì, cách tạo một bảng tổng hợp và cuối cùng là cách sử dụng bảng tổng hợp. Hãy đi sâu vào Show
bảng tổng hợp là gìMột bảng tổng hợp được sử dụng để kết nối các mối quan hệ giữa hai bảng. Laravel cung cấp một mối quan hệ <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }3 nơi bạn có thể sử dụng một bảng tổng hợp Việc sử dụng bảng tổng hợpTrong ví dụ tiếp theo, chúng ta sẽ xem lại cách liên kết bảng <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }4 và bảng <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }5 với bảng tổng hợp <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }6. Bảng Pivot cho phép bạn (trong trường hợp này) thêm nhiều <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }5 vào một <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }8 Ví dụ về Pivot TableHãy tạo hai bảng mà chúng ta cần sử dụng mối quan hệ <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }3 cho
Bảng <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }4 sẽ giống như thế này
Bảng <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }5 sẽ như thế này
Bây giờ bảng tổng hợp của chúng tôi <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }6 sẽ trông như thế này
Sử dụng Pivot TableVì vậy, bây giờ chúng ta đã xem qua cấu trúc của hai bảng và bảng <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }72 của chúng ta, hãy tìm hiểu cách sử dụng chúng Tạo Di chuyển của Bảng PivotQuá trình di chuyển sẽ giống như thế này, nó thiết lập các cột thích hợp và quan hệ khóa ngoại cho cả bảng <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }4 và <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }5. Quá trình di chuyển cũng sẽ đặt cả <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }70 và <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }71 làm khóa chính để không thể có khóa trùng lặp với cả hai khóa <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }70 và <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }71 giống nhau <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }6 Thiết lập quan hệ mô hìnhChúng tôi sẽ mở các mô hình <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }79 và <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }80 của chúng tôi và xác định các mối quan hệ cần thiết trong cả hai mô hình <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } } <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }1 Xác định bảng trung gian tùy chỉnh của bạnNếu bạn muốn xác định mô hình tùy chỉnh đại diện cho bảng tổng hợp hoặc bảng trung gian của mối quan hệ của mình, bạn có thể gọi phương thức <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }81 khi xác định mối quan hệ. Lưu ý rằng các mô hình xoay vòng <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }3 tùy chỉnh sẽ mở rộng lớp <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }83. Ví dụ: chúng tôi muốn xác định Vai trò sử dụng mô hình xoay vòng <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }84 tùy chỉnh <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }7 Truy xuất vai trò của người dùngGiả sử đối tượng <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }85 của chúng ta có nhiều đối tượng <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }86 mà nó liên quan đến. Sau khi truy cập vào mối quan hệ này, chúng ta có thể truy cập vào bảng trung gian bằng cách sử dụng thuộc tính <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }72 trên các mô hình <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }8
Tùy chỉnh tên thuộc tính <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }72Như đã đề cập ở trên, các thuộc tính từ bảng trung gian có thể được truy cập trên các mô hình bằng thuộc tính trục. Tuy nhiên, bạn có thể tùy chỉnh tên của thuộc tính này để phản ánh tốt hơn mục đích của nó trong ứng dụng của bạn Ví dụ: nếu ứng dụng của bạn chứa người dùng có thể đăng ký nhiều podcast, thì có thể bạn có mối quan hệ nhiều-nhiều giữa người dùng và podcast. Nếu trường hợp này xảy ra, bạn có thể muốn đổi tên trình truy cập bảng trung gian của mình thành <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }31 thay vì <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }72. Điều này có thể được thực hiện bằng cách sử dụng phương pháp <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }33 khi xác định mối quan hệ <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }3 Khi điều này được thực hiện, bạn có thể truy cập dữ liệu bảng trung gian bằng tên tùy chỉnh ở trên <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }7
Thêm vai trò cho người dùng<?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }1 Thêm nhiều vai trò cho người dùngBạn có thể chuyển một mảng ID vào phương thức <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }37 để thêm nhiều vai trò cho người dùng <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }3 Xóa vai trò khỏi người dùng<?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }4 Xóa nhiều vai trò khỏi người dùngĐiều này hoạt động tương tự như trên khi thêm vai trò nhưng bạn có thể chuyển một mảng ID vào phương thức <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }38 để xóa nhiều vai trò khỏi người dùng <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }0 Đồng bộ hóa vai tròCó một phương thức tên là <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }39 chấp nhận các giá trị mới dưới dạng một mảng ID và sẽ đảm nhận việc đồng bộ hóa các vai trò. Kết quả là bất kể giá trị nào trong bảng trước khi chạy <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }39, sau khi mã được chạy, sẽ chỉ có các ID <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }71 này được đính kèm với người dùng. Nó sẽ xóa bất kỳ thứ gì không có trong danh sách này nhưng có trong cơ sở dữ liệu và thêm bất kỳ thứ gì không có trong cơ sở dữ liệu nhưng có trong danh sách này <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }1 Đính kèm một vai trò với các tham số bổ sungĐôi khi bạn có thể muốn thêm các thông số bổ sung khi đính kèm một <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }71 vào một <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }8. Hãy xem một ví dụ về cách làm điều này. Trong ví dụ này, giả sử Quản trị viên đang chỉ định vai trò mới cho người dùng nhưng họ muốn chỉ định vai trò đó được thêm bởi ai (chỉ là một chuỗi để đơn giản, giả sử <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }74 là một cột trên bảng <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }6 cùng với <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }70 và <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }71) <?php namespace App\Models; use App\Models\Role; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /* * The roles that belong to this user */ public function roles() { return $this->belongsToMany(Role::class); } }2 kết thúcCó rất nhiều điều bạn có thể làm với các mối quan hệ và chúng được ghi lại trong. Mục tiêu của bài viết này là thiết lập sự hiểu biết và cách sử dụng cơ bản với các bảng tổng hợp trong Laravel |