【发布时间】:2019-01-10 19:06:36
【问题描述】:
您好,我正在更新电子商务网站的产品形式,但是当我尝试编辑详细信息时,它显示错误“未定义的变量:文件名”并且错误行是:
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!');
或者当我尝试更新图像时,它的错误是:“从空值创建默认对象”,或者错误行是:
$product->image = $filename;
这是 ProductsController 的代码:
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'));
}
【问题讨论】:
-
您没有在任何地方定义
$product,而是立即尝试通过$product->image = $filename在您的代码中使用它 -
@TimLewis 你愿意告诉我如何解决它
-
在您尝试设置属性之前将
$product定义为...例如$product = ...;然后$product->image ... -
$product = $filename;但仍然是同样的错误
标签: laravel laravel-5 eloquent