[英]can't overwrite laravel elequent output's id field
I can't overwrite the $user->id in the following code : 我无法在以下代码中覆盖$ user-> id:
$followers_list = follow::where('followed_id',$userId['userId'])
->get();
foreach($followers_list as $follower)
{
$user = myuser::find($follower->follower_id);
echo $user->id;//everything is fine
$user->id = Crypt::encrypt(['id'=> $user->id]);
echo $user->id; //it's zero for all users
array_push($followers,$user);
}
is it a rule or something in laravel eloquent to prevent such type conversions(integer to string)? 防止这种类型转换(整数到字符串)是雄辩的规则还是laravel中的某些东西?
how can I replace the id's integer value with its encrypted string? 如何将ID的整数值替换为其加密的字符串?
any help? 有什么帮助吗?
Try this: 尝试这个:
$user->attributes['id'] = Crypt::encrypt(['id'=> $user->id]);
A Laravel getter and settier in eloquent doesnt allow changing some object properties. Laravel getter和settier雄辩地不允许更改某些对象属性。 The attributes object is a wrapper of the object's real properties.
属性对象是对象实际属性的包装。
laravel will not allow to change datatype of PrimaryKey on the fly on any models, laravel不允许在任何模型上即时更改PrimaryKey的数据类型,
how do I know it? 我怎么知道 check YourProject/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:57
检查YourProject / vendor / laravel / framework / src / Illuminate / Database / Eloquent / Model.php:57
Now if you want to do it, you can call $user->setKeyType('string')
and then do your $user->id = 'hihello';
现在,如果要执行此操作,可以调用
$user->setKeyType('string')
,然后执行$user->id = 'hihello';
is it recommended? 推荐吗? I don't know!
我不知道!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.