Project Tutorials
Introduction of Laravel Setup and Installation of Laravel Project Laravel Structure Create Login Page Design Validation on Login Login with Database Display Username after Login Logout Register Design Dashboard Page Add Product List Products from Database Edit product from database Active Inactive user statusEdit product from database
In this tutorial, we are going to teach you how to create update product details.
Step 1 : Create a link of edit button for edit product details and then pass product id with link. So that we will get the product details.
<a href="{{url('/edit-product',$product->id)}}" class="btn btn-primary">Edit</a>
Step 2 : Now create a route in web.php to get product detail.
Route::get('/edit-product/{id}','MainController@edit_product');
Step 3 : Open your main controller and create a edit_product function so that we will get the details of the product with the help of ID and we will pass the product details on edit page.
eg:
function edit_product($id)
{
//get product details with the help of ID
$product = DB::table('product')
->select('*')
->where('id','=',$id)
->first();
//load edit page and pass product details on edit page
return view('edit_product',['product'=>$product]);
}
Step 4 : Create your edit page (edit_product.blade.php) in view folder then we will add product data on it so that we can update it.
Note : In this edit form, I pass the details of the product in the value field. like : - value="<?php echo $product->product_name; ?>"
<form action="#" method="post">
{{csrf_field()}}
<label>Product Name : </label>
<input type="text" class="form-control" name="product_name" value="<?php echo $product->product_name; ?>" required>
<input type="hidden" class="form-control" name="product_id" value="<?php echo $product->id; ?>" required>
<label>Product Price : </label>
<input type="text" class="form-control" name="product_price" value="<?php echo $product->product_price; ?>" required>
<label>Product Quantity : </label>
<input type="text" class="form-control" name="product_quantity" value="<?php echo $product->product_quantity; ?>" required>
<label>Status : </label>
<select name="status" class="form-control" required>
<option value="">---Select Status---</option>
<option value="1" <?php if($product->status == '1'){echo'selected';} ?>>Active</option>
<option value="0" <?php if($product->status == '0'){echo'selected';} ?>>Inactive</option>
</select>
<button class="btn btn-success" type="submit">Update Product</button>
</form>
In this from contains the hidden product id. With the help of this ID, we can update the details of product.
Like : <input type="hidden" name="product_id" value="<?php echo $product->id; ?>">
Step 5 : Create form action to work with update detail and also create method post. So that you can update product data in database.
<form action="{{url('/update-product')}}" method="post">
Step 6 : Now, open your main controller and create update_product() function and then we will write the update code in it so that we will update the details of the product in the database.
function update_product(Request $request)
{
//Get the product Id
$id = $request->input('product_id');
//Get product details from from
$values = array(
'product_name' => $request->input('product_name'),
'product_price' => $request->input('product_price'),
'product_quantity' => $request->input('product_quantity'),
'status' => $request->input('status')
);
// Update prodcut details in product table with the help product id
DB::table('product')->where('id',$id)->update($values);
//back to success message
session()->flash('msg','Product has been updated successfully.');
return redirect('main/successlogin');
}
Step 7 : Now create a route in web.php to update product detail.
Route::post('/update-product','MainController@update_product');
Result :
Source Code:
Small Laravel Project
In this project. We are providing you, how to create small project in Laravel....
Source CodeElevenstech Web Tutorials
Elevenstech Web Tutorials helps you learn coding skills and enhance your skills you want.
As part of Elevenstech's Blog, Elevenstech Web Tutorials contributes to our mission of “helping people learn coding online”.
Read More
Newsletter
Subscribe to get the latest updates from Elevenstech Web Tutorials and stay up to date