You are missing a / in your form action route:
action="{{ url('/admin/edit-product/ '.$productDetails->id) }}"
hi m trying to update a post but its not updating and it says that page not found and also the image on the edit-page is not showing. is there any solution to resolve it, here is my code:
controller:
public function editProduct(Request $request, $id=null){
if($request->isMethod('post')){
$data = $request->all();
//echo "<pre>"; print_r($data); die;
//Upload Image
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;
}
}
else{
$filename = $data['current_image'];
}
if(empty($data['description'])){
$data['description'] = '';
}
Product::where(['id'=>$id])-
>update(['status'=>$status,'category_id'=>$data ['category_id'],'product_name'=>$data['product_name'],
'product_code'=>$data['product_code'],'product_color'=>$data ['product_color'],'description'=>$
data['description'],'care'=>$data['care'],'price'=>$data['price'],'image'=> $fileName]);
return redirect()->back()->with('flash_message_success', 'Product has been edited
successfully');
}
//Get product details
$productDetails = Product::where(['id'=>$id])->first();
//Categories dropdown start
$categories = Category::where(['parent_id'=>0])->get();
$categories_dropdown = "<option value='' selected disabled>Select</option>";
foreach($categories as $cat){
if($cat->id==$productDetails->category_id){
$selected = "selected";
}
else{
$selected = "";
}
$categories_dropdown .= "<option value='".$cat->id."' ".$selected.">".$cat- >name."
</option>";
$sub_categories = Category::where(['parent_id'=>$cat->id])->get();
foreach ($sub_categories as $sub_cat) {
if($sub_cat->id==$productDetails->category_id){
$selected = "selected";
}
else{
$selected = "";
}
$categories_dropdown .= "<option value = '".$sub_cat->id."' ". $selected."> --
".$sub_cat->name."</option>";
}
}
//Categories dropdown start
return view('admin.products.edit_product')-
>with(compact('productDetails','categories_dropdown'));
}
this is form:
<form enctype="multipart/form-data" class="form-horizontal" method="post" action="{{
url('/admin/edit-product'.$productDetails->id) }}" name="edit_product" id="edit_product"
novalidate="novalidate"> {{ csrf_field() }}
<div class="control-group">
<label class="control-label">Under Category</label>
<div class="controls">
<select name="category_id" id="category_id" style="width: 220px;">
<?php echo $categories_dropdown; ?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Product Name</label>
<div class="controls">
<input type="text" name="product_name" id="product_name" value="{{
$productDetails->product_name }}">
</div>
</div>
<div class="control-group">
<label class="control-label">Product Code</label>
<div class="controls">
<input type="text" name="product_code" id="product_code" value="{{
$productDetails->product_code }}">
</div>
</div>
<div class="control-group">
<label class="control-label">Product Color</label>
<div class="controls">
<input type="text" name="product_color" id="product_color" value="{{
$productDetails->product_color }}">
</div>
</div>
<div class="control-group">
<label class="control-label">Description</label>
<div class="controls">
<textarea name="description" id="description">{{ $productDetails->description }}
</textarea>
</div>
</div>
<div class="control-group">
<label class="control-label">Price</label>
<div class="controls">
<input type="text" name="price" id="price" value="{{ $productDetails->price }}">
</div>
</div>
<div class="control-group">
<label class="control-label">Image</label>
<div class="controls">
<input type="file" name="image" id="image">
<img style="width:30px;" src="{{
asset('/images/backend_images/product/small/'.$productDetails->image) }}">
</div>
</div>
<div class="form-actions">
<input type="submit" value="Edit Product" class="btn btn-success">
</div>
</form>
Route:
Route::match(['get','post'],'/admin/edit-product/{id}','ProductsController@editProduct');
@HAXORDEVELOPER - You need to check where your image is located in your system , and compare it with the output of this:
dd(asset('/images/backend_images/product/small/'.$productDetails->image))
Please or to participate in this conversation.