Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ConorDonohoe's avatar

Column not found: 1054 Unknown column '0' in 'field list' Laravel

I have looked everywhere and I know there are questions like this out there on stack overflow and ive tried to follow them but to no joy I am trying to run an update query with an array and i keep getting unknown column '0' Whenever I run it ive tried using a for loop and I know its something to do with how the update method handles the data but I can't figure out how to make it work any and all help will be greatly appreciated

My controller:

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['created_by'] = $unsanitizedData['updatedBy'];
        $data['user_id'] = $unsanitizedData['id'];
        $data['activated'] = $unsanitizedData['cred']['activated'];
        foreach ($unsanitizedData as $st) {
            $convertedData = json_encode($st);
            $form = admin_form::where('id', '49')->update([$convertedData]);
        }
        $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);
    }
}

My model:

0 likes
2 replies
ftiersch's avatar
ftiersch
Best Answer
Level 28

Why are you converting your data to json?

You want something like this:

->update([
    'column' => 'value'
]);

Yours looks like this:

->update(["{'column':'value'}"])

So you have an array (with 0 in the key) and laravel interprets that as you want to update the column "0" with the json value.

1 like
ConorDonohoe's avatar

Thank you very much if you wouldn't mind I have one more part of the function that is an array already that I am trying to update New controller code:

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 ive tryed to apply the same logic as the other statement 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

Please or to participate in this conversation.