ConorDonohoe's avatar

Undefined index error type and name

I am currently trying loop through bested data so I can update it but when I try to loop through the inputs it gives me an undefined index error on inputs type and name.

public function saveEdit(Request $request)
{
    try {
        $unsanitizedData = $request->all();
        $data = [];
        $fid = $unsanitizedData['formId'];
        $data['form_name'] = $unsanitizedData['cred']['name'];
        $data['org'] = $unsanitizedData['cred']['organization'];
        $data['updated_by'] = $unsanitizedData['updatedBy'];
        $data['user_id'] = $unsanitizedData['id'];
        $data['activated'] = $unsanitizedData['cred']['activated'];
        $data['type'] = $unsanitizedData['cred']['inputs']['type'];
        $data['name'] = $unsanitizedData['cred']['inputs']['name'];

        foreach ($unsanitizedData as $st) {
            $form = admin_form::where('id', '49')->update([
                'form_name'=> $data['form_name'],
                'org'=> $data['org'],
                'updated_by' => $data['updated_by'],
                'user_id' => $data['user_id'],
                'activated' => $data['activated']
                ]);
        }
         $input['form_id'] = $form->id;
         foreach ($unsanitizedData['cred']['inputs'] as $input) {
             admin_form_fields::where('form_id', $fid)->update([
                 'type' => $data['type'],
                 'name' => $data['name']
             ]);
         }
        $res['status'] = true;
        $res['message'] = 'Success';
        return response($res, 200);
    } catch (\Illuminate\Database\QueryException $ex) {
        $res['status'] = false;
        $res['message'] = $ex->getMessage();
        return response($res, 500);
    }
}

this is what the data strucute being sent to the API looks like

{cred: {name: "Test namegghh", organization: "Test Orggghhh", activated: [0],…}, formId: "49", id: 29,…} cred: {name: "Test namegghh", organization: "Test Orggghhh", activated: [0],…} activated: [0] inputs: [{type: "email", name: "Filehh"}] 0: {type: "email", name: "Filehh"} name: "Test namegghh" organization: "Test Orggghhh" formId: "49" id: 29 updatedBy: "test"

The inputs is an array of inputs with a name and type eg: type: file, name: CV a form can have as many inputs as it wants I've tried to apply the same logic as the other function but I am getting an undefined index type and name error do you know how I could get around this is it because its nested? thank you very much for your help.

0 likes
1 reply
Sinnbeck's avatar

Please format all your code using triple `

Please or to participate in this conversation.