简体   繁体   English

SQLSTATE[HY000]:一般错误:1364 字段“care”没有默认值

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

I'm new to Laravel and trying to add products from admin panel to index page, firstly it was working fine but now its showing me error SQLSTATE[HY000]: General error: 1364 Field 'care' doesn't have a default value , is there any solution of this problem?我是 Laravel 的新手,并试图将产品从管理面板添加到索引页面,首先它工作正常,但现在它向我显示错误SQLSTATE[HY000]: General error: 1364 Field 'care' doesn't have a default value ,这个问题有什么解决办法吗?

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))

This is ProductsController:这是 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'));
}

Just add your value: care to your fillable只需增加您的价值: care您的fillable

protected $fillable = [
        'care',
];

Or just make it nullable in your migration:或者只是在您的迁移中使其nullable

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

Does it work?它有效吗?

$fillable needs to match the database column names not the form names. $fillable 需要匹配数据库列名称而不是表单名称。

Also, some fields need to be nullable incase the user does not have it.此外,某些字段需要可以为空,以防用户没有它。 Exp.经验。 address地址

it seems like you are not providing the value for the path.似乎您没有提供路径的值。 You can either provide the value for the path column or go to the phpMyAdmin and set the default value to NULL.您可以提供路径列的值,也可以转到 phpMyAdmin 并将默认值设置为 NULL。 Or you can edit your migration file for the photos table and set the path column to be the default of null.或者,您可以编辑照片表的迁移文件,并将路径列设置为默认值 null。

$table->string('path')->nullable() and then rerun migrations: $table->string('path')->nullable()然后重新运行迁移:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLSTATE [HY000]:常规错误:1364字段“照片”在laravel 5.5中没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'photo' doesn't have a default value in laravel 5.5 SQLSTATE[HY000]:一般错误:1364 字段“contactId”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'contactId' doesn't have a default value SQLSTATE [HY000]:常规错误:1364字段“性别”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'gender' doesn't have a default value SQLSTATE [HY000]:常规错误:1364字段“ starting_balance”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'starting_balance' doesn't have a default value SQLSTATE [HY000]:常规错误:1364字段“ reply_text”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'reply_text' doesn't have a default value SQLSTATE [HY000]:一般错误:1364 字段“agent_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'agent_id' doesn't have a default value SQLSTATE[HY000]:一般错误:1364 字段“parent_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'parent_id' doesn't have a default value SQLSTATE[HY000]:一般错误:1364 字段“名称”没有默认值 laravel 5.5 - SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value laravel 5.5 SQLSTATE[HY000]:一般错误:1364 字段“country_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'country_id' doesn't have a default value SQLSTATE[HY000]:一般错误:1364 字段“已接受”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'accepted' doesn't have a default value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM