SQLSTATE[HY000]: General error: 1364 Field 'category_name' doesn't have a default value
SQLSTATE[HY000]: General error: 1364 Field 'category_name' doesn't have a default value
我正在尝试将数据插入数据库,但它显示:
SQLSTATE[HY000]: General error: 1364 Field 'category_name' doesn't
have a default value (SQL: insert into categories
(updated_at
,
created_at
) values (2019-07-23 02:34:10, 2019-07-23 02:34:10))
我的控制器:
public function store(Request $request)
{
//dd($request->all());
$request->validate([
'category_name' => 'required',
'category_description' => 'required',
'category_slug' => 'required',
'category_image' => 'required|image',
]);
$path = $request->file('category_image');
$image = $path->getClientOriginalName();
$path->move(public_path('images/backend_images/category_images'));
$category = Category::create($request->all());
return redirect()->back();
}
我的模型:
protected $fillable = [
'categry_name', 'categry_description', 'categry_slug', 'categry_image'
];
这是一个数据库table:
您 $fillable
中的 categry_name
拼错了。它缺少 e
根据您的控制器,您的模型存在拼写错误。
您必须将模型更改为
protected $fillable = [
'category_name', 'category_description', 'category_slug', 'category_image'
];
如果您使用数据库迁移,则必须像这样更新所有列:
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('category_name');
$table->string('category_description');
$table->string('category_slug');
$table->string('category_image');
$table->timestamps();
});
在设计您的数据库时,要非常精确,否则在您不确定它们是否是强制性的列中使用“nullable”。迁移中的可空声明是:
$table->string('category_name')->nullable();
根据您的控制器,您的型号存在拼写错误
class categories extends Model {
protected $fillable = ['category_name', 'category_description', 'category_slug', 'category_image'];
//only the field names inside the array can be mass-assign
}
更多详情:What does “Mass Assignment” mean in Laravel
我正在尝试将数据插入数据库,但它显示:
SQLSTATE[HY000]: General error: 1364 Field 'category_name' doesn't have a default value (SQL: insert into
categories
(updated_at
,created_at
) values (2019-07-23 02:34:10, 2019-07-23 02:34:10))
我的控制器:
public function store(Request $request)
{
//dd($request->all());
$request->validate([
'category_name' => 'required',
'category_description' => 'required',
'category_slug' => 'required',
'category_image' => 'required|image',
]);
$path = $request->file('category_image');
$image = $path->getClientOriginalName();
$path->move(public_path('images/backend_images/category_images'));
$category = Category::create($request->all());
return redirect()->back();
}
我的模型:
protected $fillable = [
'categry_name', 'categry_description', 'categry_slug', 'categry_image'
];
这是一个数据库table:
$fillable
中的 categry_name
拼错了。它缺少 e
根据您的控制器,您的模型存在拼写错误。 您必须将模型更改为
protected $fillable = [
'category_name', 'category_description', 'category_slug', 'category_image'
];
如果您使用数据库迁移,则必须像这样更新所有列:
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('category_name');
$table->string('category_description');
$table->string('category_slug');
$table->string('category_image');
$table->timestamps();
});
在设计您的数据库时,要非常精确,否则在您不确定它们是否是强制性的列中使用“nullable”。迁移中的可空声明是:
$table->string('category_name')->nullable();
根据您的控制器,您的型号存在拼写错误
class categories extends Model {
protected $fillable = ['category_name', 'category_description', 'category_slug', 'category_image'];
//only the field names inside the array can be mass-assign
}
更多详情:What does “Mass Assignment” mean in Laravel