【发布时间】:2018-09-21 23:33:55
【问题描述】:
我必须使用ajax将数据插入数据库,现在它将数据插入数据库,但不上传图像。
ajax 代码
$(document).on("click","#save", function(){
jQuery.ajax({
method :'post',
url:''+APP_URL+'/products/add_product',
data:$("#product_form").serialize(),
success: function(response){
$("#product_list").load("<?php echo url('products/tblproducts');?>").fadeIn("slow")
}
});
});
“product_form”是表单的 ID。
路线
Route::post('products/add_product','admin\ProductsController@add_new_product');
控制器功能
public function add_new_product(Request $request){
try{
DB::beginTransaction();
$product_image = NULL;
if ($request->hasFile('product_photo')){
$ext = $request->product_image->getClientOriginalExtension();
$product_image = uniqid().".".$ext;
$file = $request->file('product_image');
$destinationPath = public_path("products");
$file->move($destinationPath, $product_image);
}
$bdata['product_photo']=$product_image;
$bdata['product_name']= $request->item_name;
$bdata['product_barcode']= $request->barcode;
DB::table('tbl_products')->insert($bdata);
DB::commit();
$this->CreateMessages('add');
}catch(\Exception $v){
DB::rollback();
$this->CreateMessages('not_added');
throw $v;
}
}
【问题讨论】: