Search code examples
laraveleloquent

I Have error "SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list'"


When i update table, laravel is broken On another site same code work well


                        $flatBuilder = Lot::where('lotid', $lotid);
                        $arFlat = $flatBuilder->first();

                        $arFields[] = array(
                            'lotid' => $lotid,
                            'lot' => $lot,
                            'type' => $type,
                            'status' => $status,
                            'price' => $price,
                            'pricesq' => $pricesq,
                            'area' => $area,
                            'floor' => $floor,
                            'height' => $height
                        );

                        if ($arFlat) {
                            $flatBuilder->update($arFields);
                        } else {
                            Lot::create($arFields);
                        }

update dont work... Where is my mistake? Need help!


Solution

  • Since you're already assigning array value to the $arFields, you don't need to initialize it as empty array. Change your code to this (remove [] from $$arFields)

    $arFields = array(
        'lotid' => $lotid,
        'lot' => $lot,
        'type' => $type,
        'status' => $status,
        'price' => $price,
        'pricesq' => $pricesq,
        'area' => $area,
        'floor' => $floor,
        'height' => $height
    );
    

    Result:

    array:9 [
      "lotid" => 1
      "lot" => 1
      "type" => 1
      "status" => 1
      "price" => 1
      "pricesq" => 1
      "area" => 1
      "floor" => 1
      "height" => 1
    ]
    

    The way you wrote it, your array has one element, which has a value of array:

    array:1 [
      0 => array:9 [
        "lotid" => 1
        "lot" => 1
        "type" => 1
        "status" => 1
        "price" => 1
        "pricesq" => 1
        "area" => 1
        "floor" => 1
        "height" => 1
      ]
    ]
    

    And $flatBuilder->update($arFields) is using that element (0 in this case), to find the relevant column.