When i update a relation, e.g. update parent_id on Child (Child belongsTo Parent, Parent hasMany Child) and respond with the Child->Parent object, somehow the returned Parent is still the old one. I think this is because the Parent is already loaded at that time.

现在,我想处理这些关系,以便再次从数据库中获取新的父对象.

Is there a way to unload the loaded relationships? Like you can lazy-load by using model->load('relation'), can you also unload it again?

谢谢!

推荐答案

您可以通过取消设置magic属性(至少在Laravel 5.3及更高版本中)来卸载关系.

用法:

unset($model->relation);

是什么让这项工作(来自模型类):

public function __unset($key)
{
    unset($this->attributes[$key], $this->relations[$key]);
}

它的作用与$model->setRelations([])相同,但针对特定的关系(而不是卸载所有关系).

Laravel相关问答推荐

Laravel中如何动态更改路由链接?

如何在Laravel中创建自定义单项集合

如何让 Laravel 的 Collection 表现得像一个流?

我如何通过 laravel 在高图中针对不同的时间范围进行烛台数据分组

Laravel 模型事件:delete() 不会从存储中删除文件

Laravel & Docker:无法打开流或文件/var/www/html/storage/logs/laravel.log:无法打开流:权限被拒绝

Laravel:转义LIKE子句?

Eloquent 的集合方法,例如 only 或 except 返回一个空集合

如何判断是否在laravel中设置了cookie?

路由模型绑定不起作用

Laravel 随机排序

Laravel 存储文件的公共 url

Filesystem.php 中的 ErrorException

测试指向外部站点的重定向链接

Laravel 迁移 - 删除列

Grammar::parameterize() 必须是数组类型

如何在 Laravel 5.2 中使用 OR 条件将多个参数传递给中间件

Laravel 在域和子域中共享 cookie 检测问题

如何获取 Laravel 块的返回值?

如果在 Laravel 5.1 中找不到路由,则显示 404 页面