php - Laravel:使用 belongsTo() 向我的模型添加意外属性
问题描述
我正在使用 Lumen 编写一个 REST API。我的示例有 2 个模型User
和Post
. Post
model 使用该方法belongsTo
获取User
创建此帖子的模型。我的目标是定义一个访问器,这样我就可以像那样获取帖子的作者用户名Post::find($id)->author
。所以根据文档我这样做:
Post.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $table = 'posts';
protected $appends = ['author'];
protected $fillable = [
'title',
'description'
];
protected $hidden = [
'user_id',
'created_at',
'updated_at'
];
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
public function getAuthorAttribute()
{
return $this->user->username;
}
}
现在 getter 工作得很好,我可以很容易地得到 given 的作者Post
。
但是,如果我尝试Post
在 JSON 响应中返回 ,它也会返回给我奇怪的属性,这些属性user
似乎来自我user()
调用 a 的方法belongsTo()
:
return response()->json(Post::find(2), 200);
{
"id": 2,
"title": "Amazing Post",
"description": "Nice post",
"author": "FooBar",
"user": {
"id": 4,
"username": "FooBar"
}
}
如果我使用attributesToArray()
它按预期工作:
return response()->json(Post::find(2)->attributesToArray(), 200);
{
"id": 2,
"title": "Amazing Post",
"description": "Nice post",
"author": "FooBar"
}
此外,如果我删除 gettergetAuthorAttribute()
和$appends
声明,我不会得到意外的user
属性。
但是我不想每次都使用这个方法,如果我想返回我所有的Post
使用,它不会使它工作:
return response()->json(Post::all(), 200);
有人知道我为什么要使用这个附加属性belongsTo
吗?
解决方案
这种行为是因为性能。当你
$post->user
第一次调用时,Laravel 会从数据库中读取并保存以$post->relation[]
备下次使用。所以下次 Laravel 可以从数组中读取它并防止再次执行查询(如果你在多个地方使用它会很有用)。另外,用户也是一个属性, 当您调用或时,Laravel 会合并
$attributes
和排列在一起$relations
$model->toJson()
$model->toArray()
Laravel 的模型源代码:
public function toArray() { return array_merge($this->attributesToArray(), $this->relationsToArray()); } public function jsonSerialize() { return $this->toArray(); }
推荐阅读
- swift - 协议扩展中解码器的初始化给出了错误“在'self.init'调用或分配给'self'之前使用'self'”
- verilog - Verilog 对 1 位信号按位异或
- java - 从时间持续时间到时间字符串的转换
- python - python中根据类名快速查找要导入的模块
- vim - 用fugitive浏览git树时,去父目录的快捷方式是什么?
- python - 如何抓取需要登录并填写验证码的网站的数据
- javascript - 取消选中复选框时从 javascript 中删除 NaN 字
- javascript - React js中的React Bootstrap SweetAlert不起作用
- sql - 查询无重合三表连接
- algorithm - 为什么循环正在替换已经推入向量的树节点