php - SQLSTATE[HY000] : General error: 1364 Field 'care' doesn't have a default value

标签 php laravel laravel-5 eloquent

我是 Laravel 的新手,正在尝试将产品从管理面板添加到索引页面,首先它工作正常,但现在它显示错误 SQLSTATE[HY000]: General error: 1364 Field 'care' doesn'没有默认值,这个问题有解决办法吗?

SQLSTATE[HY000]: General error: 1364 Field 'care' doesn't have a
default value (SQL: insert into `products` (`product_name`,
`product_code`, `product_color`, `description`, `price`, `image`,
`updated_at`, `created_at`) values (Filter, ASD111, CLR, ASDF, 1000, 
85525.png, 2019-01-10 17:18:33, 2019-01-10 17:18:33))

这是 ProductsController:

  public function addProduct(Request $request){

  if($request->isMethod('post')){
    $data = $request->all();
    //echo "<pre>"; print_r($data); die;

    $product = new Product;
    $product->product_name = $data['product_name'];
    $product->product_code = $data['product_code'];
    $product->product_color = $data['product_color'];
    if(!empty($data['description'])){
      $product->description = $data['description'];
    }else{
    $product->description = '';         
    }
    $product->price = $data['price'];

    // Upload Image
    if($request->hasFile('image')){
      $image_tmp = Input::file('image');
      if($image_tmp->isValid()){
        $extension = $image_tmp->getClientOriginalExtension();
        $filename = rand(111,99999).'.'.$extension;
        $large_image_path = 
        'images/backend_images/products/large/'.$filename;
        $medium_image_path = 
        'images/backend_images/products/medium/'.$filename;
        $small_image_path = 
        'images/backend_images/products/small/'.$filename;
        // Resize Images
        Image::make($image_tmp)->save($large_image_path);
        Image::make($image_tmp)->resize(600,600)->save($medium_image_path);
        Image::make($image_tmp)->resize(300,300)->save($small_image_path);

        // Store image name in products table
        $product->image = $filename;
        }
       }

    $product->save();

        return redirect('/admin/view-products')- 
   >with('flash_message_success','Product has been added successfully!');
  }


  return view('admin.products.add_product');
}

public function editProduct(Request $request, $id=null){

  if($request->isMethod('post')){
    $data = $request->all();
    //echo "<pre>"; print_r($data); die;


    if($request->hasFile('image')){
      $image_tmp = Input::file('image');
      if($image_tmp->isValid()){
        $extension = $image_tmp->getClientOriginalExtension();
        $filename = rand(111,99999).'.'.$extension;
        $large_image_path = 'images/backend_images/products/large/'.$filename;
        $medium_image_path = 'images/backend_images/products/medium/'.$filename;
        $small_image_path = 'images/backend_images/products/small/'.$filename;
        // Resize Images
        Image::make($image_tmp)->save($large_image_path);
        Image::make($image_tmp)->resize(600,600)->save($medium_image_path);
        Image::make($image_tmp)->resize(300,300)->save($small_image_path);

        // Store image name in products table
        $product->image = $filename;
      }
    }

    if(empty($data['description'])){
          $data['description'] = '';
        }


    Product::where(['id'=>$id])->update(['product_name'=>$data['product_name'],'product_code'=>$data['product_code'],'product_color'=>$data['product_color'],'description'=>$data['description'],'price'=>$data['price'],'image'=>$fileName]);
    return redirect()->back()->with('flash_message_success','Product updated successfully!');


  }

  //Get product details
  $productDetails = Product::where(['id'=>$id])->first();

  return view('admin.products.edit_product')->with(compact('productDetails'));
}

最佳答案

只需将您的值:care 添加到您的 fillable

protected $fillable = [
        'care',
];

或者只是在您的迁移中使其 nullable:

$table->string('care')->nullable();

有效吗?

关于php - SQLSTATE[HY000] : General error: 1364 Field 'care' doesn't have a default value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54133994/

相关文章:

php - 获取最近一小时的记录,如果最近一小时没有记录,则获取最近 20 条记录

php - 如何从域中获知 Feed url?

Laravel 向多个收件人发送邮件并添加多个文件

php - 在 PHP 中向 MySQL 插入多行(单个查询): Prepare-Execute vs. 准备-绑定(bind)-执行

php - 使用带有复选框和连接表的日期范围过滤数据

php - 数据插入前数据库数据字段检查

php - Laravel中数据透视表的访问关系

php - Laravel Blade : @stop VS @show VS @endsection VS @append

php - 拉维尔;如何使 setLocale 永久化?

php - net::ERR_CONNECTION_REFUSED 与 Nginx 和 Laravel 5