SQLSTATE [42S22]:找不到列:1054'where子句'中的未知列'id'

时间:2017-06-10 04:56:58

标签: laravel

我收到以下错误SQLSTATE [42S22]:找不到列:1054'where子句'中的未知列'id'(SQL:选择count(*)作为来自categories的聚合name =男性和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 个答案:

答案 0 :(得分:0)

您定义的主键是category_id 然后你用

  

CATEGORY_ID

而不是

  

ID

查询中的

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