如何使用Laravel更新我的userData中的一个值

huangapple go评论99阅读模式
英文:

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.');
}

huangapple
  • 本文由 发表于 2023年5月25日 22:41:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定