- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用 Laravel 5 构建一个简单的时间跟踪应用程序,其中我有两个 Laravel 模型,一个称为“用户”,一个称为“周”
关系很简单:
周.php:
public function user()
{
return $this->belongsTo('App\User');
}
用户.php:
function weeks()
{
return $this->hasMany('App\Week');
}
现在,User.php 文件还有一个名为“getCurrentWeek()”的简单辅助/新颖函数,顾名思义,它返回当前周
function getCurrentWeek()
{
$possibleWeek = $this->weeks->sortByDesc('starts')->all();
if(count($possibleWeek) != 0)
{
return $possibleWeek[0];
}
else
{
return null;
}
}
我的问题是:如果我为用户创建第一周并将其附加/关联到用户,如下所示:
$week = new Week;
//Omitted: Setting so attributes here ...
$week->user_id = $user->id;
$weeks->save()
$user->weeks()->save($week);
然后调用$user->getCurrentWeek();
方法,该方法返回null
,虽然已经创建了新的一周,但与用户相关已保存到数据库中。在我看来,getCurrentWeek()
的预期行为是返回新创建的周。
我在这里对 Eloquent 有什么误解/做错了什么?
最佳答案
关系属性在第一次访问时被延迟加载。加载后,它们不会自动刷新关系中添加或删除的记录。
因为您的 getCurrentWeek()
函数使用了关系属性,所以这段代码可以工作:
$week = new Week;
// setup week ...
$user->weeks()->save($week);
// $user->weeks attribute has not been accessed yet, so it will be loaded
// by the first access inside the getCurrentWeek method
dd($user->getCurrentWeek());
但是,这段代码将不起作用:
// accessing $user->weeks lazy loads the relationship
echo count($user->weeks);
// relate a new record
$week = new Week;
// setup week ...
$user->weeks()->save($week);
// $user->weeks inside the method will not contain the newly related record,
// as it has already been lazy loaded from the access above.
dd($user->getCurrentWeek());
您可以修改 getCurrentWeek()
方法以使用关系方法 ($this->weeks()
) 而不是属性 ($this ->weeks
),这将始终命中数据库,或者您可以在添加或删除记录后重新加载关系(使用 load()
方法)。
更改 getCurrentWeek()
方法以使用关系方法 weeks()
(@Bjorn 提供的更新方法)
function getCurrentWeek()
{
return $this->weeks()->orderBy('starts', 'desc')->first();
}
或者,使用 load()
方法刷新关系:
// accessing $user->weeks lazy loads the relationship
echo count($user->weeks);
// relate a new record
$week = new Week;
// setup week ...
$user->weeks()->save($week);
// reload the weeks relationship attribute
$user->load('weeks');
// this will now work since $user->weeks was reloaded by the load() method
dd($user->getCurrentWeek());
关于php - 保存后的 Laravel 关系可用性(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39520072/
是否可以使用 JHipster 5(带有 Spring Boot 2)?我注意到在发行说明中提到了 JHipster 5 的工作,但看不到任何关于如何安装/运行 5 的“预览”版本的信息。 最佳答案
使用多个显示器时,有没有办法拆分 Xcode 界面? 我有 2 台显示器,但仍然没有找到一种简单的方法在超过 1 台显示器上使用它(左侧的 Storyboard,右侧的代码) - 就像在 Androi
我正在尝试处理 uwp 应用程序中的 xml,我添加了引用和用法: using System.Xml; using System.Xml.XPath; 但是 private XmlDocument d
JSunit 真的是 Junit 到 Javascript 的直接端口吗?所以我听说过,但我想知道为什么我的公司没有使用它。我的公司有很多聪明人,所以如果我们不使用它,我想知道 Jsunit 作为 j
当使用原始 TCP 套接字时,需要进行消息帧化,如 here 所述,带有长度前缀或分隔符。 我今天遇到了“SOCK_SEQPACKET”套接字选项,它显然可以几乎透明地进行消息框架。 目前“SOCK_
我在一个小型开发小组工作。我们正在构建和改进我们的产品。 半年前我们无法考虑更高的特性,比如可用性,因为我们的产品有很多问题。许多错误、高技术债务、低性能和其他问题使我们无法专注于可用性。 随着时间的
是否可以通过编程方式控制@RestController来启用或禁用它?我不想只在每个 @RequestMapping 方法中编写代码来执行某种 if (!enabled) { return 404Ex
azure 的 blob 在被新版本覆盖时是否可供下载? 根据我使用 Cloud Storage Studio 的测试,下载被阻止,直到覆盖完成,但是我的测试来自同一台计算机,因此我无法确定这是正确的
我一直认为(但出于某种原因从未实践过)动态生成且仅包含一个项目的下拉菜单应该自动选择该项目。这与我观察到的典型方法相反,即在顶部创建一个空白条目,您仍然必须与菜单交互以进行单个可用选择。 例如,当我登
我正在为我的兄弟编写一个基于 Web 的小实用程序,他需要合并 CSV 文件中的列。我知道这样的事情肯定存在于某个地方,但这很大程度上是因为这是一个有趣的小练习。 无论如何,我正在尝试找出最好/最简洁
如您所知,pinterest api 现在似乎已关闭。( api.pinterest.com )在此站点中:http://tijn.bo.lt/pinterest-api ,它表示只读访问仍然有效。到
我想为公寓可用性应用程序制作日历 View 。我的想法是从 MySQL 数据库获取日期范围(不可用日期)并将其显示在日历 View 中。我的日历 View 应该有 3 种不同的样式: 可用日期 不可用
我究竟应该如何配置我的广播接收器以使我的应用程序响应 wifi 可用性的变化。不过,当应用程序未运行时,我并不真正关心对此使用react。 我在这里试图完成的是,当应用程序运行时,我需要使本地数据库与
在我的网络应用程序中,我有 parent 和保姆。 保姆将声明工作时间(时间间隔),该工作时间将存储在工作时间表中。 家长也会每隔一段时间表达自己的需求(暂时不需要存储)。 所以我设计了这样的可用性表
概览 我希望在 CloudKit 无法访问时收到通知。 CloudKit 可能无法访问的可能场景: CloudKit 服务器不可访问 互联网连接不可用。 迄今为止所做的尝试: 观察 CKAccount
好的,我有我为核心数据创建的类 LoginPass.h 然后我有头等舱 FirstClass.h 然后我需要在 SecondClass 中使用这些类,我在其中使用 @class 声明它们。头文件 Se
例如 linux kernel-2.6.32-279 没有在/usr/include/asm-generic/socket.h 中定义 SO_REUSEPORT 但 kernel-2.6.32-431
我正在尝试安装的工具说: MySQL server data directory needs to reside on an LVM volume 如何检查我的数据目录是否在 LVM 上? 最佳答案
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Programmatically find device support GPS or not? 如何检查设
我听说汽车行业有一种叫做 MISRA C 的东西。 其他高可靠性/可用性/安全性行业的相关标准是什么,例如 空间 飞机 银行/金融 汽车 医疗 国防/军事 ??? -亚当 最佳答案 查看戈达德太空飞行
我是一名优秀的程序员,十分优秀!