Laravel 模型关联 「 预加载 」中 with () 方法的功能的示例及说明

laravel 模型关联 「 预加载 」 ->with()功能的示例

1 模型关联说明:在动态模型 Status中,指明一条微博动态属于一个用户 User

<?php
.
.
// 动态模型status    关联belongsTo    用户模型user
class Status extends Model {
    public function user() {
        return $this->belongsTo(User::class);
    }
}

2.不使用预加载,调用数据时:

// 获取微博动态的发布用户     不使用预加载                             
$statuses = App\Status::all();       

// foreach之前运行 dd($statuses);
foreach ($statuses as $status) {          
    echo $status->user->name;              
}                                      

dd()打印的结果 ,请注意关注点1 #relations: []为空

LengthAwarePaginator {#319 ▼
  #total: 89
  #lastPage: 12
  #items: Collection {#430 ▼
    #items: array:8 [▼
      0 => Status {#431 ▶}
      1 => Status {#432 ▶}
      2 => Status {#438 ▼
        #fillable: array:1 [▶]
        #connection: "mysql"
        #table: "statuses"
        .
        .
        #appends: []
        #dispatchesEvents: []
        #observables: []
        #relations: []     ==============================>>关注点1
        #touches: []
        +timestamps: true
        #hidden: []
        #visible: []
        #guarded: array:1 [▶]
      }
    ]
  }
  .
  .
  #options: array:2 [▶]
}

3.使用预加载with('user')调用数据时:

// 获取微博动态的发布用户     使用预加载 with('user')                                  
$statuses = App\Status::with('user')->get();       

// foreach之前运行 dd($statuses);
foreach ($statuses as $status) {          
    echo $status->user->name;              
}                                      

dd()打印的结果 ,请注意下面标注的 关注点1#relations: [] 不为空,关注点2 含有对应的user的全部信息

LengthAwarePaginator {#329 ▼
  #total: 89
  #lastPage: 12
  #items: Collection {#425 ▼
    #items: array:8 [▼
      0 => Status {#432 ▶}
      1 => Status {#433 ▶}
      2 => Status {#439 ▼
        #fillable: array:1 [▶]
        #connection: "mysql"
        .
        .
        #dispatchesEvents: []
        #observables: []
        #relations: array:1 [▼        // =========================>>>>>关注点1
          "user" => User {#445 ▼
            #fillable: array:3 [▶]
            #hidden: array:2 [▶]
            #casts: array:1 [▶]
            .
            .
            #attributes: array:11 [▼     // ======================>>>>>关注点2
              "id" => 5
              "name" => "娄亮"
              "email" => "doloribus98@example.com"
              "email_verified_at" => "2019-10-05 19:49:31"
              "password" => "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi"
              "remember_token" => "N4nrVyeptW"
              "created_at" => "2007-07-14 13:34:01"
              "updated_at" => "2007-07-14 13:34:01"
              "is_admin" => 0
              "activation_token" => null
              "activated" => 1
            ]
            #original: array:11 [▶]
          .
          .
            #rememberTokenName: "remember_token"
          }
        ]
        #touches: []
        +timestamps: true
        #hidden: []
        #visible: []
        #guarded: array:1 [▶]
      }
    ]
  }
  #options: array:2 [▶]
}
  1. 对比之后的结论和总结
    • 不带with('user')方法时,由关注点1可知 relations[]为空,$statuses 只包动态信息
    • with('user')时,由关注点2可知,每一个status 都有与其对应一个user属性(含包用户的全部信息)
    • 重点:预加载方法with()的意思就是在foreach 运行获取name之前,已经全部把每一个动态所对应的所有的 name 已经全部准备好了,因此可以有如下结论:
    • 不带with('user')只有动态信息没有动态对应的用户信息,foreach每遍历一次,就要通过 $status->user->name 查询一次数据库。
    • with('user'),拥有动态信息和用户信息,foreach 只对$statuses遍历,不需要查询数据库。
本作品采用《CC 协议》,转载必须注明作者和本文链接
Practice makes perfect !
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!