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

时间:2019-01-10 17:28:39

标签: php laravel laravel-5 eloquent

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

这是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'));
}

3 个答案:

答案 0 :(得分:1)

只需将您的值public partial class InfoSettings : UserControl { public InfoSettings() { InitializeComponent(); } private void settingBtn_Click(object sender, EventArgs e) { Form1 form1 = (Form1) this.Parent; form1.buttonHandler(sender); } } 添加到您的care

fillable

或者在迁移中将其设置为protected $fillable = [ 'care', ];

nullable

行得通吗?

答案 1 :(得分:0)

$ fillable需要匹配数据库列名而不是表单名。

此外,如果用户没有字段,则某些字段需要为空。经验值地址

答案 2 :(得分:0)

似乎您没有提供路径的值。您可以提供path列的值,也可以转到phpMyAdmin并将默认值设置为NULL。或者,您可以编辑photos表的迁移文件,并将path列设置为默认值null。

$table->string('path')->nullable(),然后重新运行迁移: