英文:
How to update one value in my userData using Laravel
问题
I am trying to update specific value in my user data.
User data can look like this:
User : {
role : [0, 1],
name : "x",
CommonId : 'xxx',
skills : [
{
id : 1,
name : 'x',
level : 'null'
},
{
id : 2,
name : 'x',
level : 'good
},
.........
]
First, I have to access the specific user who want to make an update ($user) => (working)
Then, i need to access the skills data and found the one to update ($matchedSkill) => (working)
public function UpdateLevelSkill(Request $request)
{
$level = $request->get('level');
$commonId = $request->get('commonId');
$userId = $request->get('userId');
$user = User::where('microsoftId', $userId)->first();
$skills = $user->skills;
$matchedSkill = (object) [];
foreach ($skills as $skill) {
if ($skill['CommonId'] === $commonId) {
$matchedSkill = $skill;
break;
}
};
return response()->json($matchedSkill);
}
When I return matchedSkill
, I got the object skills I want to update.
But then, if I add
$matchedSkill->Level = $level;
$user->save();
I got error:
Attempt to read property "Level" on array"
even if matchedSkill
is an array.
How can I update the skill ?
I have tried to use ['Level'] with no success
I have also tried to add condition (if matchedSkill ...)
英文:
I am trying to update specific value in my user data.
User data can look like this :
User : {
role : [0, 1],
name : "x",
CommonId : 'xxx',
skills : [
{
id : 1,
name : 'x',
level : 'null'
},
{
id : 2,
name : 'x',
level : 'good
},
.........
]
First, I have to access the specific user who want to make an update ($user) => (working)
Then, i need to access the skills data and found the one to update ($matchedSkill) => (working)
public function UpdateLevelSkill(Request $request)
{
$level = $request->get('level');
$commonId = $request->get('commonId');
$userId = $request->get('userId');
$user = User::where('microsoftId', $userId)->first();
$skills = $user->skills;
$matchedSkill = (object) [];
foreach ($skills as $skill) {
if ($skill['CommonId'] === $commonId) {
$matchedSkill = $skill;
break;
}
};
return response()->json($matchedSkill);
}
When I return matchedSkill
, I got the object skills I want to update.
But then, if I add
$matchedSkill->Level = $level;
$user->save();
I got error:
> Attempt to read property "Level" on array"
even if matchedSkill
is an array.
How can I update the skill ?
I have tried to use ['Level']
with no success
I have also tried to add condition (if matchedSkill ...)
答案1
得分: 0
以下是您提供的代码的中文翻译部分:
更新时考虑到matchedSkill是一个数组(正在工作中):
public function UpdateLevelSkill(Request $request)
{
$level = $request->get('level');
$commonId = $request->get('commonId');
$userId = $request->get('userId');
$user = User::where('microsoftId', $userId)->first();
$skills = $user->skills;
$matchedSkillIndex = null;
foreach ($skills as $index => $skill) {
if ($skill['CommonId'] === $commonId) {
$matchedSkillIndex = $index;
break;
}
}
if ($matchedSkillIndex !== null) {
$matchedSkill = $skills[$matchedSkillIndex];
$matchedSkill['Level'] = $level;
$skills[$matchedSkillIndex] = $matchedSkill;
$user->skills = $skills;
$user->save();
return response()->json($matchedSkill);
}
return response()->json('技能未找到。');
}
请注意,这只是提供了代码的翻译部分,不包括其他内容。如果您需要进一步的信息或有其他问题,请随时提出。
英文:
Updating with consideration that matchedSkill is an array (working):
public function UpdateLevelSkill(Request $request)
{
$level = $request->get('level');
$commonId = $request->get('commonId');
$userId = $request->get('userId');
$user = User::where('microsoftId', $userId)->first();
$skills = $user->skills;
$matchedSkillIndex = null;
foreach ($skills as $index => $skill) {
if ($skill['CommonId'] === $commonId) {
$matchedSkillIndex = $index;
break;
}
}
if ($matchedSkillIndex !== null) {
$matchedSkill = $skills[$matchedSkillIndex];
$matchedSkill['Level'] = $level;
$skills[$matchedSkillIndex] = $matchedSkill;
$user->skills = $skills;
$user->save();
return response()->json($matchedSkill);
}
return response()->json('Skill not found.');
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论