MuhammadUmar's avatar

Undefined variable: filename

hi m working on update products form of ecommerce site but when i try to edit details then it shows error "Undefined variable: fileName" and error line is:

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

this is code of 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 = $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'));
  }
0 likes
4 replies
realrandyallen's avatar

The variable is $filename but in your update array you are trying to use $fileName

1 like
majad.shohagh's avatar

You May try it..


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

realrandyallen's avatar
Level 44

@DEVELOPER_ - The $filename variable doesn’t exist unless an image is uploaded and it’s successfully processed - you may want to set that variable at the top as null for a default or restructure your code

3 likes

Please or to participate in this conversation.