有没有办法在 saving 或 updating 事件中查看模型属性的旧/以前值?
例如.是否有以下可能:
User::updating(function($user){if ($user->username != $user->old->username) doSomething();});好吧,我是偶然发现的,因为它目前不在文档中...
有一个 getOriginal() 方法可以返回原始属性值的数组:
User::updating(function($user){if ($user->username != $user->getOriginal('username')) {做一点事();}//如果你需要多个属性,你可以使用://$originalAttributes = $user->getOriginal();//$originalUsername = $originalAttributes['username'];});<块引用>
小心,在 Laravel 7 之前 getOriginal 忽略属性类型转换.
Is there a way to see the old/previous value of a model's attribute in its saving or updating event?
eg. Is something like the following possible:
User::updating(function($user)
{
if ($user->username != $user->old->username) doSomething();
});
Ok, I found this quite by chance, as it's not in the documentation at present...
There is a getOriginal() method available which returns an array of the original attribute values:
User::updating(function($user)
{
if ($user->username != $user->getOriginal('username')) {
doSomething();
}
// If you need multiple attributes you may use:
// $originalAttributes = $user->getOriginal();
// $originalUsername = $originalAttributes['username'];
});
Be careful, prior to Laravel 7
getOriginalignores attribute type casting.
这篇关于在 Eloquent 模型事件中获取先前的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
MySQLi准备好的语句&foreach 循环MySQLi prepared statement amp; foreach loop(MySQLi准备好的语句amp;foreach 循环)
mysqli_insert_id() 是从整个服务器还是从同一用户获Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是从整个服务器还是从同一用户获取记录?)
PHP MySQLi 无法识别登录信息PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 无法识别登录信息)
mysqli_select_db() 需要 2 个参数mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 个参数)
Php mysql pdo 查询:用查询结果填充变量Php mysql pdo query: fill up variable with query result(Php mysql pdo 查询:用查询结果填充变量)
MySQLI 28000/1045 用户“root"@“localhost"的访问MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用户“root@“localhost的访问被拒绝)