0
votes

I get the following error SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select count(*) as aggregate from categories where name = Men and id <> 1)

Category.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
        protected $fillable = ['name'];

        protected $primaryKey = 'category_id';

    public function products()
    {
        return $this->hasMany(Product::class);
    }
}

CategoryController.php

public function edit($id)
{
    $category = Category::findOrFail($id);
    return view('admin.category.edit', compact('category'));
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    $category = Category::findOrFail($id);
    $this->validate($request, [
        'name' => 'required|string|max:255|unique:categories,name,'. $category->category_id
    ]);

    $category->update($request->all());
    Flash::success('Kategori berhasil diubah');
    return redirect()->route('category.index');
}
1

1 Answers

0
votes

You define primary key is category_id then you use

category_id

instead of

id

in the query

SQL: select count(*) as aggregate from categories where name = Men and id <> 1