我有产品创建过程中的图片上传功能。这是控制器中createSubmit的功能Laravel查询正在寻找不存在的列
public function productsCreateSubmit()
{
$product = new Product;
$product->title = Input::get('title');
$product->price = Input::get('price');
if (Input::hasFile('image')) {
$file = Input::file('image');
$filename = str_random(20);
$file->move('uploads/', $filename . '.' . $file->getClientOriginalExtension());
$imagePath = 'uploads/' . $filename . '.' . $file->getClientOriginalExtension();
$image = Image::make($imagePath);
if ($image->width() > 200) {
$image->resize(200, null, function ($constraint) {
$constraint->aspectRatio();
});
}
if ($image->height() > 200) {
$image->resize(null, 200, function ($constraint) {
$constraint->aspectRatio();
});
}
$image->save();
} else {
$imagePath = 'default.png';
}
$product->image = $imagePath;
$product->save();
return Redirect::to('/admin/products');
}
当我点击提交时,我收到数据库中的列没有默认值的错误。
SQLSTATE[HY000]: General error: 1364 Field 'file_id' doesn't have a default value (SQL: insert into
products
(title
,price
,image
) values (test, 1, uploads/awy3CvVHLajER1E1gfpu.jpg))
的问题是,我没有在表中这样的列。这是产品的型号
class Product extends Eloquent
{
protected $table = 'products';
protected $primaryKey = 'product_id';
public $timestamps = false;
}
laravel如何决定插入/寻找此字段?
您可以添加产品表的迁移文件 –
我没有为此迁移。它是直接在phpmysql中创建的 – Ivan
即使您没有在控制器中使用它,您是否确定数据库中没有该列? –