laravel5.6 ORM 關聯模型,一對一和一對多


Laravel5.6 關聯模型的操作,主要是一對一,一對多,多對多等操作.下面示例主要解析前面兩個操作用法比較常用.(操作和用法TP5類似)

將關聯查詢使用語法hasOne、hasMany、belongsTo進行一個舉例說明?
hasOne:有一個,加上主謂語應該是, A 有一個 B
hasMany:有很多, A 有很多 B
belongsTo:屬於, A 屬於 B

demo示例:
假設Users模型和News模型存在關聯關系.兩表sql和假設數據如下:
users.sql sql文件
 1 DROP TABLE IF EXISTS `users`;
 2 CREATE TABLE `users` (
 3   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 4   `name` char(30) NOT NULL DEFAULT '' COMMENT '名稱',
 5   `created_at` timestamp NULL DEFAULT NULL COMMENT '創建時間',
 6   `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新時間',
 7   PRIMARY KEY (`id`)
 8 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用戶表';
 9 
10 -- ----------------------------
11 -- Records of users
12 -- ----------------------------
13 INSERT INTO `users` VALUES ('1', 'admin_01', '2018-11-19 10:06:17', '2018-11-19 10:06:22');
14 INSERT INTO `users` VALUES ('2', 'admin_02', '2018-11-19 10:09:27', '2018-11-19 10:09:34');
15 INSERT INTO `users` VALUES ('3', 'admin_03', '2018-11-19 11:36:56', '2018-11-19 11:37:00');

news.sql sql文件
 1 CREATE TABLE `news` (
 2   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 3   `users_id` int(10) NOT NULL DEFAULT '1' COMMENT '用戶表id',
 4   `author` char(30) NOT NULL DEFAULT '' COMMENT '作者',
 5   `content` text NOT NULL COMMENT '內容',
 6   `created_at` timestamp NULL DEFAULT NULL COMMENT '創建時間',
 7   `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新時間',
 8   PRIMARY KEY (`id`)
 9 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='新聞表';
10 
11 -- ----------------------------
12 -- Records of news
13 -- ----------------------------
14 INSERT INTO `news` VALUES ('1', '1', ' Ning', 'Hello World 01', '2018-11-19 10:08:04', '2018-11-19 10:07:59');
15 INSERT INTO `news` VALUES ('2', '2', 'Wang', 'Hello World 02', '2018-11-19 10:11:01', '2018-11-19 10:11:08');
16 INSERT INTO `news` VALUES ('3', '3', 'Li', 'Hello World 03', '2018-11-19 11:37:57', '2018-11-19 11:37:59');
17 INSERT INTO `news` VALUES ('4', '3', 'Hong', 'Hello World 04', '2018-11-19 15:02:26', '2018-11-19 15:02:29');

一對一:
例如:一個 Users 模型有一個與之關聯的 News 模型.
Users.php Users模型
 1 <?php
 2 
 3 namespace App\Model\Eloquent\Admin;
 4 
 5 use Illuminate\Database\Eloquent\Model;
 6 
 7 class Users extends Model
 8 {
 9     protected $table = 'users';
10 
11     /**
12      * 關聯news表
13      */
14     public function newsMethod()
15     {
16         //hasOne($related, $foreignKey = null, $localKey = null)
17         //第一個參數為對應的model, 第二個參數默認為model對應的表的外鍵, 第三個參數默認為當前模型對應的表的主鍵
18         return $this->hasOne('App\Model\Eloquent\Admin\News','users_id', 'id');
19     }
20 }

 

News.php News模型
 1 <?php
 2 
 3 namespace App\Model\Eloquent\Admin;
 4 
 5 use Illuminate\Database\Eloquent\Model;
 6 
 7 class News extends Model
 8 {
 9     protected $table = 'news';
10 
11     /**
12      * 相對關聯users表
13      */
14     public function usersMethod()
15     {
16         //belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null)
17         //第一個參數為model, 先講第四個參數,默認為調用belongsTo()的方法名字, 第二個參數默認為第四個參數加上 '_id', 第三個參數默認為model的對應表的主鍵
18         return $this->belongsTo('App\Model\Eloquent\Admin\Users','users_id','id');
19     }
20 }

 

關聯查詢 -- 查詢news表關聯id=1,一條數據
1 $data = Users::find(1)->newsMethod->toArray();
結果打印:


相對關聯查詢 -- 查詢users表關聯id=2,一條數據
1 $data = News::find(2)->usersMethod->toArray();
結果打印:

 
一對多
一對多關聯:是定義單個模型擁有多個其它模型的關聯關系.
Users.php Users模型
 1 <?php
 2 
 3 namespace App\Model\Eloquent\Admin;
 4 
 5 use Illuminate\Database\Eloquent\Model;
 6 
 7 class Users extends Model
 8 {
 9     protected $table = 'users';
10 
11     /**
12      * 關聯news表 假設一對多
13      */
14     public function newsMethodMany()
15     {
16         return $this->hasMany('App\Model\Eloquent\Home\News','users_id', 'id');
17     }
18 }

關聯查詢 -- 查詢users和news兩表關聯id=3,兩條數據
1 $data = Users::find(3)->newsMethodMany->toArray();
結果打印:


查詢存在的關聯關系操作語法.
with:類似於 SQL 中的 left join 注:渴求加載查指定字段, id字段是必須列出
1 $data = News::with('usersMethod:id,name')->get()->toArray();

結果打印:

 
has:類似於 SQL 中的 inner join
1 $data = News::has('usersMethod')->get()->toArray();
whereHas:inner join 之后,可以補充查詢條件
1 $data = News::whereHas('usersMethod', function ($query) {
2     $query->where('author', '=', 'Hong');
3 })->get()->toArray();
結果打印:



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM