从空值“ laravel中的错误”创建默认对象

时间:2019-01-10 19:06:36

标签: laravel laravel-5 eloquent

他正在处理电子商务网站的更新产品表格,但是当我尝试编辑详细信息时,它显示错误“未定义变量:fileName”,错误行是:

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

2 个答案:

答案 0 :(得分:0)

“未定义的变量:文件名”

您的变量中有一个错字。将$fileName更改为$filename

答案 1 :(得分:0)

我猜你需要做这样的事情

<script>
var path = document.querySelector('#line');
var pathLength = path.getTotalLength();

path.style.strokeDasharray = pathLength + ' ' + pathLength;
path.style.strokeDashoffset = pathLength;

window.addEventListener("scroll", function(e) {
    var scrollPercentage = (document.documentElement.scrollTop +             
document.body.scrollTop) /               
(document.documentElement.scrollHeight - 
document.documentElement.clientHeight);

var drawLength = pathLength * scrollPercentage;
path.style.strokeDashoffset = pathLength - drawLength;

});
</script>

您还在代码中的$ fileName中定义了什么位置?