- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我只是想更好地了解 Laravel 的 Eloquent/Model 是如何处理关系的。
假设我定义了一个关系,其中每个 Post 都有一个 Author 并且 Post 类有一个方法来获取与之关联的作者对象:
public function author() {
return $this->belongsTo( 'App\User', 'author_id' );
}
现在调用帖子的 author() 方法将根据帖子的 author_id 字段返回作者。我的问题是:每次使用该方法时,Laravel 是否都会进行查询?以下代码是否会向数据库请求两次数据?
<a href="{{ route('user',$post->author->slug) }}">{{ $post->author->name }}</a>
最佳答案
这个问题,有例子,有完整的记录right here .
其中提到:
When accessing Eloquent relationships as properties, the relationship data is "lazy loaded". This means the relationship data is not actually loaded until you first access the property.
你可以
When querying, you may specify which relationships should be eager loaded using the with method:
因此,当您调用 $post->author
时,您只进行了一次查询,并重复使用了之前查询中的相同数据。
但是,如果您也这样做,比方说,多个帖子并遍历它们以询问他们的作者,每个请求都会是一个新的查询。除非模型有 protected $with = ['author']
属性,或者查询中包含 with 以急切地加载它。
$post = App\Post::with('author')->find(2)->get();
所有的数据都会被预先加载,并且只有一个执行的查询。
关于php - 像 belongsTo 这样的 Laravel 关系方法是否总是查询数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39506764/
class Owner { static hasMany = Dog } class Sitter { static hasMany = Dog } class Dog { s
如果我有以下型号: module.exports = function (sequelize, DataTypes) { var WorkingCalendar = sequelize.defin
我有这样的问题 - 有 3 个表,例如 Clients、Hats、Tshirts。每个客户都有一顶帽子和一件 T 恤关系。我是这样做的: ` ClientsModel: var $name
我使用 Laravel 5.6,我有 3 个模型: 地区 商店 商家 区域有很多商店: public function stores() { return $this->hasMany('Be
我已经在几个地方看到过要“远离”这一点,但是唉 - 这就是我的数据库的构建方式: class Album extends Eloquent { // default connection
如何制作域类以在 Grails 中创建“共享”数据但不“属于”另一个数据的表?例如,我想制作 2 张 table : 具有 2 个主要列的人员表 名称 - 技能 拉里 - 跳跃,大笑,拳击 curl
当子类可能属于父类中的两个属性之一(但不能同时属于两者)时,我无法理解 Grails 中的 belongsTo - hasMany 关系的概念。 例如: class Store { Integ
我创建了Child域,并为其添加了2个父级。我声明为一位 parent 。我在belongsTo约束的帮助下声明了另一个 parent 。 打包多个 parent class Child {
场景一: 我有两个类: 发表 : class Post { String content Date dateCreated static constraints = { content(blank:
我以为答案是很基本的,但是我对Grails还是很陌生,并且在其他任何地方都很难找到我的问题的答案。 基本上,我有两个域类,用户和事件。事件与用户具有“belongsTo”关系,而用户与事件具有“has
要在 Grails 中创建一对一关系,我可以执行以下操作: class Person { static hasOne = [address: Address] } 在这种情况下,地址表具有其人
有人告诉我,要完成将 $belongsTo 映射到非主键,我会将外键设置为 false 并由另一个论坛上的某人(实际上是 IRC)设置条件。但是,我不认为我这样做是正确的。下面是我正在尝试的 $bel
所以我有这个模型,“票”,其中包含(除其他外)“category_id”列。 为简化起见,以下是我的数据的外观: 门票表 ID 标题 类别编号 1 问题 #1 2 2 问题#2 4 3 问题 #3 1
class Batch extends Eloquent { public function coupons() { return $this->hasMany('Coupon
谁能向我解释在ManyToMany关系中belongsTo的作用是什么?对于OneToOne关系,这很明显:如果删除了“OneToMany”侧(主表)中的记录,则相应的“ManyToOne”(子表)也
我有一些模型正在尝试关联。 一种模型是 Item,一种是 Slide,另一种是 Asset。 项目下方有多个幻灯片。 Assets 基本上是已上传的文件(图像、mp3 等),幻灯片是显示 Assets
我正在尝试保存属于另一个模型的记录。然而,我得到一个错误,指出外键丢失。 CakePHP 文档显示了此工作的示例,但我无法让它工作。 这是表格: echo $this->Form->creat
好的必填信息: DEBUG: ------------------------------- DEBUG: Ember : 1.3.1 DEBUG: Ember Data : 1.0.0-b
我有一个顾问表,它有一个链接到“specilaties”表的外键“specialty_id”。 class Consultant extends AppModel { public $belo
我正在处理 belongsTo 关系,但需要知道如何指定要关联的字段。 我有一个报告和一个汽车表。 汽车表以 ID 字段作为 UI。当对汽车进行报告时,我希望从列表中删除该汽车,以便我使用示波器。 在
我是一名优秀的程序员,十分优秀!