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

shahr's avatar
Level 10

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

TypeController.php

public function edit(Product $product, Warranty  $warranty, Type $type)
{
    $colors = $product->colors()->get();
    $price = Type::query()->where('warranty_id', $warranty->id)->pluck('price', 'color_id')->toArray();
    return view('Admin.types.edit', compact('product', 'warranty', 'type', 'colors', 'price'));
}

edit.blade.php

<form action="{{ route('products.warranties.types.update', [$product, $warranty, $type]) }}" method="post">
    @csrf
    @method('PUT')
    <input type="hidden" name="new_date" value="{{ $type->date }}">
    @foreach($colors as $color)
        <div class="mb-3 row">
            <label for="color{{ $color->id }}" class="col-sm-2 col-form-label">{{ $color->color_name }}</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" id="color{{ $color->id }}" name="color[{{ $color->id }}]" value="{{ $price[$color->id] }}">
            </div>
        </div>
    @endforeach
    <div class="row">
        <div class="col-md-6 mt-3">
            <button type="submit" class="btn btn-primary btn-lg">ذخیره</button>
        </div>
    </div>
</form>

public function update(Product $product, Warranty $warranty, Type $type, Request $request)
{
    $date = $request->post('new_date');
    $timestamp = Jalalian::fromFormat('Y/m/d', $date)->getTimestamp();
    $colors = $request->post('color');
    $data = [];
    foreach ($colors as $key => $value) {
        $data[] = [
            'warranty_id' => $warranty->id,
            'color_id' => $key,
            'date' => $date,
            'time' => $timestamp,
            'price' => $value,
        ];
    }
    $type->update($data);
    return redirect()->route('products.warranties.index', [$product->id, $warranty->id]);
}

When I add dd($data); I see this message.

public function update(Product $product, Warranty $warranty, Type $type, Request $request)
{
    $date = $request->post('new_date');
    $timestamp = Jalalian::fromFormat('Y/m/d', $date)->getTimestamp();
    $colors = $request->post('color');
    $data = [];
    foreach ($colors as $key => $value) {
        $data[] = [
            'warranty_id' => $warranty->id,
            'color_id' => $key,
            'date' => $date,
            'time' => $timestamp,
            'price' => $value,
        ];
    }
    dd($data);
array:2 [▼
  0 => array:5 [▼
    "warranty_id" => 1
    "color_id" => 1
    "date" => "1402/01/01"
    "time" => 1679356800
    "price" => "225000"
  ]
  1 => array:5 [▼
    "warranty_id" => 1
    "color_id" => 3
    "date" => "1402/01/01"
    "time" => 1679356800
    "price" => "552000"
  ]
]

It has an error.

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: update types set 0 = {"warranty_id":1,"color_id":1,"date":"1402/01/01","time":1679356800,"price":"225000"}, 1 = {"warranty_id":1,"color_id":3,"date":"1402/01/01","time":1679356800,"price":"552000"}, types.updated_at = 2023-03-20 11:53:42 where id = 1)

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The error message indicates that there is an unknown column '0' in the field list. This is likely caused by the update method trying to set the values of the data array as columns in the types table. To fix this, we need to modify the update method to use the updateOrCreate method instead of update.

Here's the updated code for the update method:

public function update(Product $product, Warranty $warranty, Type $type, Request $request)
{
    $date = $request->post('new_date');
    $timestamp = Jalalian::fromFormat('Y/m/d', $date)->getTimestamp();
    $colors = $request->post('color');
    foreach ($colors as $key => $value) {
        Type::updateOrCreate(
            ['warranty_id' => $warranty->id, 'color_id' => $key, 'date' => $date],
            ['time' => $timestamp, 'price' => $value]
        );
    }
    return redirect()->route('products.warranties.index', [$product->id, $warranty->id]);
}

In this updated code, we use the updateOrCreate method to update or create a new record in the types table. We pass an array of attributes to match against existing records, and another array of attributes to update or create a new record with.

This should fix the error and allow the update method to update the types table with the new data.

1 like

Please or to participate in this conversation.