laravel中的多對多關係詳解

數據表之間是縱橫交叉、相互關聯的,laravel的一對一,一對多比較好理解,官網介紹滴很詳細了,在此我就不贅述啦,重點我記下多對多的關係php

一種常見的關聯關係是多對多,即表A的某條記錄經過中間表C與表B的多條記錄關聯,反之亦然。好比一個用戶有多種角色,反之一個角色對應多個用戶。laravel

爲了測試該關聯關係,咱們沿用官網的用戶角色示例:數據結構

須要三張數據表:usersroles 和 role_userrole_user 表按照關聯模型名的字母順序命名(這裏role_user是中間表),而且包含 user_id 和 role_id兩個列。測試

多對多關聯經過編寫返回 belongsToMany 方法返回結果的方法來定義。廢話不說多,直接上數據結構:this

1:建立一個角色表roles,並添加一些初始化數據:spa

SET FOREIGN_KEY_CHECKS=0;code

-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `users_email_unique` (`email`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;對象

-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES ('1', 'admin', 'admin@163.com', '$2y$10$J/yXqscucanrHAGZp9G6..Tu1Md.SOljX3M8WrHsUdrgat4zeSuhC', 'ilocXtjZJwhrmIdLG1cKOYegeCwQCkuyx1pYAOLuzY2PpScQFT5Ss7lBCi7i', '2016-04-21 16:26:23', '2016-12-14 09:29:59');
INSERT INTO `users` VALUES ('2', 'baidu', '10940370@qq.com', '$2y$10$2A5zJ4pnJ5uCp1DN3NX.5uj/Ap7P6O4nP2BaA55aFra8/rti1K6I2', null, '2016-04-22 06:48:10', '2016-04-22 06:48:10');
INSERT INTO `users` VALUES ('3', 'fantasy', '1009@qq.com', '', null, '2017-06-14 10:38:57', '2017-06-15 10:39:01');blog

2:建立一個角色表roles,並添加一些初始化數據:token

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for roles
-- ----------------------------
DROP TABLE IF EXISTS `roles`;
CREATE TABLE `roles` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

-- ----------------------------
-- Records of roles
-- ----------------------------
INSERT INTO `roles` VALUES ('1', '超級版主', '2016-04-21 16:26:23', '2016-12-14 09:29:59');
INSERT INTO `roles` VALUES ('2', '司令', '2016-04-22 06:48:10', '2016-04-22 06:48:10');
INSERT INTO `roles` VALUES ('3', '軍長', '2017-06-14 10:38:57', '2017-06-15 10:39:01');
INSERT INTO `roles` VALUES ('4', '司長', '2017-06-07 10:41:41', '2017-06-15 10:41:51');
INSERT INTO `roles` VALUES ('5', '團戰', '2017-06-22 10:41:44', '2017-06-28 10:41:54');
INSERT INTO `roles` VALUES ('6', '小兵', '2017-06-22 10:41:47', '2017-06-22 10:41:56');

3:建立一箇中間表role_user用於記錄users表與roles表的對應關係,並添加一些初始化數據:

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for role_user
-- ----------------------------
DROP TABLE IF EXISTS `role_user`;
CREATE TABLE `role_user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `role_id` int(11) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of role_user
-- ----------------------------
INSERT INTO `role_user` VALUES ('1', '1', '2', '2017-06-07 11:42:13', '2017-06-21 11:32:16');
INSERT INTO `role_user` VALUES ('2', '1', '3', '2017-06-07 11:32:13', '2017-06-07 11:22:13');
INSERT INTO `role_user` VALUES ('3', '2', '4', '2017-06-07 11:32:13', '2017-06-07 11:12:13');
INSERT INTO `role_user` VALUES ('4', '1', '5', '2017-06-07 11:32:13', '2017-06-07 11:22:13');
INSERT INTO `role_user` VALUES ('5', '3', '6', '2017-06-07 11:32:13', '2017-06-07 11:52:13');
INSERT INTO `role_user` VALUES ('6', '3', '2', '2017-06-07 11:32:13', '2017-06-07 11:42:13');
INSERT INTO `role_user` VALUES ('7', '2', '2', '2017-06-07 11:42:13', '2017-06-07 11:52:13');

注意咱們定義中間表的時候沒有在結尾加s而且命名規則是按照字母表順序,將role放在前面,user放在後面,而且用_分隔,這一切都是爲了適應Eloquent模型關聯的默認設置:在定義多對多關聯的時候若是沒有指定中間表,Eloquent默認的中間表使用這種規則拼接出來。

建立一個Role模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * Class Role
 * @package App\Models
 * @mixin \Eloquent
 */
class Role extends Model
{


}

而後咱們在 User 模型上定義 roles 方法:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * Class User
 * @package App\Models
 * @mixin \Eloquent
 */
class User extends Model
{
    /**
     * 用戶角色
     */
    public function roles()
    {
        return $this->belongsToMany('App\Models\Role');
    }
}

注:正如咱們上面提到的,若是中間表不是role_user,那麼須要將中間表做爲第二個參數傳入belongsToMany方法,若是中間表中的字段不是user_idrole_id,這裏咱們姑且將其命名爲$user_id$role_id,那麼須要將$user_id做爲第三個參數傳入該方法,$role_id做爲第四個參數傳入該方法,若是關聯方法名不是roles還能夠將對應的關聯方法名做爲第五個參數傳入該方法。

接下來咱們在控制器中編寫測試代碼:

<?php
$user = User::find(1); $roles = $user->roles; echo '用戶'.$user->name.'所擁有的角色:'; foreach($roles as $role) echo $role->name.' '; //對應輸出爲:用戶admin所擁有的角色:司令  軍長  團戰  

 固然,和全部其它關聯關係類型同樣,你能夠調用roles 方法來添加條件約束到關聯查詢上:

User::find(1)->roles()->orderBy('name')->get();

正如前面所提到的,爲了肯定關聯關係鏈接表的表名,Eloquent 以字母順序鏈接兩個關聯模型的名字。不過,你能夠重寫這種約定 —— 經過傳遞第二個參數到 belongsToMany 方法:

return $this->belongsToMany('App\Models\Role', 'user_roles');

除了自定義鏈接表的表名,你還能夠經過傳遞額外參數到 belongsToMany 方法來自定義該表中字段的列名。第三個參數是你定義關聯關係模型的外鍵名稱,第四個參數你要鏈接到的模型的外鍵名稱:

return $this->belongsToMany('App\Models\Role', 'user_roles', 'user_id', 'role_id');

定義相對的關聯關係

要定義與多對多關聯相對的關聯關係,只需在關聯模型中調用一下 belongsToMany 方法便可。咱們在 Role 模型中定義 users 方法:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * Class Role
 * @package App\Models
 * @mixin \Eloquent
 */
class Role extends Model
{
    /**
     * 角色用戶
     */
    public function users()
    {
        return $this->belongsToMany('App\Models\User');
    }

}

 正如你所看到的,定義的關聯關係和與其對應的User 中定義的如出一轍,只是前者引用 App\Models\Role,後者引用App\Models\User,因爲咱們再次使用了 belongsToMany 方法,全部的經常使用表和鍵自定義選項在定義與多對多相對的關聯關係時都是可用的。

測試代碼以下:

$role = Role::find(2);
$users = $role->users;
echo '角色#'.$role->name.'下面的用戶:';
foreach ($users as $user) 
  echo $user->name.' ';//對應輸出爲:角色#司令下面的用戶:admin fantasy baidu 

正如你看到的,處理多對多關聯要求一箇中間表。Eloquent 提供了一些有用的方法來與這個中間表進行交互,例如,咱們假設 User 對象有不少與之關聯的 Role 對象,訪問這些關聯關係以後,咱們可使用這些模型上的pivot 屬性訪問中間表字段:

$roles = User::find(1)->roles;
foreach ($roles as $role) 
    echo $role->pivot->role_id.'<br>';//對應輸出爲:2 3 5

 注意咱們獲取到的每個 Role 模型都被自動賦上了 pivot 屬性。該屬性包含一個表明中間表的模型,而且能夠像其它 Eloquent 模型同樣使用。

默認狀況下,只有模型主鍵才能用在 pivot 對象上,若是你的 pivot 表包含額外的屬性,必須在定義關聯關係時進行指定:

return $this->belongsToMany('App\Models\Role')->withPivot('column1', 'column2');

好比咱們修改role_user表增長一個字段 username 數據以下:

修改模型User:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * Class User
 * @package App\Models
 * @mixin \Eloquent
 */
class User extends Model
{
    /**
     * 用戶角色
     */
    public function roles()
    {
        //return $this->belongsToMany('App\Models\Role');
        return $this->belongsToMany('App\Models\Role')->withPivot('username');
    }
}

 測試代碼以下:

$user = User::find(1);
foreach ($user->roles as $role) 
  echo $role->pivot->username;//對應輸出爲:馬特馬特2馬特3

若是你想要你的 pivot 表自動包含created_at 和 updated_at 時間戳,在關聯關係定義時使用 withTimestamps 方法:

return $this->belongsToMany('App\Models\Role')->withTimestamps();

 經過中間表字段過濾關聯關係

你還能夠在定義關聯關係的時候使用 wherePivot 和 wherePivotIn 方法過濾belongsToMany 返回的結果集:

return $this->belongsToMany('App\Models\Role')->withPivot('username')->wherePivot('username', '馬特2');
//return $this->belongsToMany('App\Models\Role')->wherePivotIn('role_id', [1, 2]);

測試代碼以下:

$user = User::find(1);
print_r($user->roles->toArray());

以上對應輸出:

Array
(
    [0] => Array
        (
            [id] => 3
            [name] => 軍長
            [created_at] => 2017-06-14 10:38:57
            [updated_at] => 2017-06-15 10:39:01
            [pivot] => Array
                (
                    [user_id] => 1
                    [role_id] => 3
                    [username] => 馬特2
                )

        )

)

若是你想要你的pivot表自動包含created_atupdated_at時間戳,在關聯關係定義時使用withTimestamps方法:

return $this->belongsToMany('App\Models\Role')->withTimestamps();

這一節我先講到這裏啦,下一節我將討論更復雜的三種關聯關係及其在模型中的定義及使用。未完待續...

相關文章
相關標籤/搜索