I hope you are doing well. I am new with laravel. I have a problem with a code for adding a product. The problem is exactly in product_image. I can't have the image and I keep receiving the message "Product added Successfully! without image"
public function save_product(Request $request)
{
$data=array();
$data['product_name']=$request->product_name;
$data['category_id']=$request->category_id;
$data['manufacture_id']=$request->manufacture_id;
$data['product_short_description']=$request->product_short_description;
$data['product_long_description']=$request->product_long_description;
$data['product_price']=$request->product_price;
$data['product_size']=$request->product_size;
$data['product_image']=$request->product_image;
$data['product_color']=$request->product_color;
$data['publication_status']=$request->publication_status;
$image=$request->file('product_image');
if ($image) {
$image_name=str_random(20);
$ext=strtolower($image->getClientOriginalExtension());
$image_full_name=$image_name.'.'.$ext;
$upload_path='image/';
$image_url=$upload_path.$image_full_name;
$success=$image->move($upload_path,$image_full_name);
if ($success) {
$data['product_image']=$image_url;
DB::table('tbl_products')->insert($data);
Session::put('message','Product added Successfully!');
return Redirect::to('/add-product');
}
//echo "<pre>";
//print_r($data);
//echo "</pre>";
//exit();
}
$data['product_image']='';
DB::table('tbl_products')->insert($data);
Session::put('message','Product added Successfully! without image');
return Redirect::to('/add-product');
}
trying to solve the problem. I added in the if statement this code to see what I have in $data:
echo "<pre>";
print_r($data);
echo "</pre>";
exit();
But I had nothing. just blank page then I added the same code before the last 4 lines. I received what in the array and data["product_image"] was empty.
This is the form (i did not forget to put enctype):
<form class="form-horizontal" action="{{ url('/save-product')}}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<fieldset>
<div class="control-group">
<label class="control-label" for="date01">Product Name</label>
<div class="controls">
<input type="text" class="input-xlarge" name="product_name" required="">
</div>
</div>
<div class="control-group">
<label class="control-label" for="selectError3">Category name</label>
<div class="controls">
<select id="selectError3" name="category_id">
<option>Select Category</option>
<?php
$all_published_category=DB::table('tbl_category')
->where('publication_status',1)
->get();
foreach($all_published_category as $v_category) {?>
<option>{{$v_category->category_name}}</option>
<?php } ?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="selectError3">Brand name</label>
<div class="controls">
<select id="selectError3" name="manufacture_id">
<option>Select Brand</option>
<?php
$all_published_manufacture=DB::table('tbl_manufacture')
->where('publication_status',1)
->get();
foreach($all_published_manufacture as $v_manufacture) {?>
<option>{{$v_manufacture->manufacture_name}}</option>
<?php } ?>
</select>
</div>
</div>
<div class="control-group hidden-phone">
<label class="control-label" for="textarea2">Product Short Description</label>
<div class="controls">
<textarea class="cleditor" name="product_short_description" rows="3" required=""></textarea>
</div>
<div class="control-group hidden-phone">
<label class="control-label" for="textarea2">Product Long Description</label>
<div class="controls">
<textarea class="cleditor" name="product_long_description" rows="3" required=""></textarea>
</div>
<div class="control-group">
<label class="control-label" for="date01">Product Price</label>
<div class="controls">
<input type="text" class="input-xlarge" name="product_price" required="">
</div>
</div>
<div class="control-group">
<label class="control-label" for="fileInput">Image</label>
<div class="controls">
<input class="input-file uniform_on" name="product_image" id="product_image" type="file">
</div>
</div>
<div class="control-group">
<label class="control-label" for="date01">Product Size</label>
<div class="controls">
<input type="text" class="input-xlarge" name="product_size" required="">
</div>
</div>
<div class="control-group">
<label class="control-label" for="date01">Product Color</label>
<div class="controls">
<input type="text" class="input-xlarge" name="product_color" required="">
</div>
</div>
<div class="control-group hidden-phone">
<label class="control-label" for="textarea2">Publication status </label>
<div class="controls">
<input type="checkbox" name="publication_status" value="1">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Add product</button>
<button type="reset" class="btn">Cancel</button>
</div>
</fieldset>
</form>
thank you in advance!
Check your code
<input class="input-file uniform_on" id="product_image" type="file">
There is no name attributes. Add it and problem solved.
<input class="input-file uniform_on" name="product_image" id="product_image" type="file">