"Updated AdminController, added update and delete product functionality, modified products.blade.php, and added datatables to adminheader and adminfooter"

This commit is contained in:
UronShrestha 2024-07-14 12:25:49 +05:45
parent f37e18f125
commit 5af3b7ef9c
9 changed files with 245 additions and 148 deletions

View File

@ -54,6 +54,8 @@ class AdminController extends Controller
// return redirect()->back()->with('success', 'Product Added Successfully');
// // return view('Dashboard.addNewProduct');
// }
//mass assignment for adding new product
public function addNewProduct(Request $request)
{
$validated = $request->validate([
@ -77,4 +79,69 @@ class AdminController extends Controller
return redirect()->back()->with('success', 'Product Added Successfully');
}
//normal assignment for update
// public function updateProduct(Request $request)
// {
// $validated = $request->validate([
// 'name' => 'required',
// 'price' => 'required|numeric',
// 'description' => 'required',
// 'file' => 'image|mimes:jpeg,png,jpg,gif,svg|max
// :2048',
// 'quantity' => 'required|numeric',
// 'category' => 'required',
// 'type' => 'required',
// ]);
// if ($request->hasFile('file')) {
// $file = $request->file('file');
// $fileName = time() . '_' . $file->getClientOriginalName();
// $file->move(public_path('uploads/products'), $fileName);
// $validated['picture'] = $fileName;
// }
// $product = Product::find($request->id);
// $product->name = $validated['name'];
// $product->price = $validated['price'];
// $product->description = $validated['description'];
// $product->quantity = $validated['quantity'];
// $product->category = $validated['category'];
// $product->type = $validated['type'];
// $product->save();
// return redirect()->back()->with('success', 'Product Updated Successfully');
// }
//mass assignment for update
public function updateProduct(Request $request)
{
$validated = $request->validate([
'name' => 'required',
'price' => 'required|numeric',
'description' => 'required',
'quantity' => 'required|numeric',
'category' => 'required',
'type' => 'required',
'file' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$product = Product::find($request->input('id'));
if ($request->hasFile('file')) {
$file = $request->file('file');
$fileName = time() . '_' . $file->getClientOriginalName();
$file->move(public_path('uploads/products'), $fileName);
$validated['picture'] = $fileName;
}
$product->update($validated);
return redirect()->back()->with('success', 'Product Updated Successfully');
}
public function deleteProduct($id)
{
$product = Product::find($id);
$product->delete();
return redirect()->back()->with('success', 'Product Deleted Successfully');
}
}

View File

@ -3,7 +3,7 @@
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Pagination\Paginator; //add this too
class AppServiceProvider extends ServiceProvider
{
/**
@ -19,6 +19,6 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot(): void
{
//
// Paginator::useBootstrap(); //this is the bootstrap code
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 560 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

View File

@ -57,148 +57,6 @@
<!-- Modal body -->
<div class="modal-body">
{{-- <form class="needs-validation" action="{{ route('addNewProduct') }}"
method="POST" novalidate>
@csrf
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name"
required>
<div class="invalid-feedback">
Please enter your name.
</div>
</div>
<div class="form-group">
<label for="email">Picture</label>
<input type="file" class="form-control" id="image" name="image"
required>
<div class="invalid-feedback">
Please enter image.
</div>
</div>
<div class="form-group">
<label for="email">Description</label>
<input type="text" class="form-control" id="description"
name="description" required>
<div class="invalid-feedback">
Please enter description.
</div>
</div>
<div class="form-group">
<label for="email">Price</label>
<input type="text" class="form-control" id="price" name="price"
required>
<div class="invalid-feedback">
Please enter price.
</div>
</div>
<div class="form-group">
<label for="email">Quantity</label>
<input type="text" class="form-control" id="quantity"
name="quantity" required>
<div class="invalid-feedback">
Please enter quantity.
</div>
</div>
<div class="form-group">
<label for="category">Category</label>
<select name="category" class="form-control" id="category"
name="category" required>
<option value=" ">Select Category</option>
<option value="accessories ">Accessories </option>
<option value="clothing ">Clothing </option>
<option value="shoes ">Shoes </option>
</select>
</div>
<div class="form-group">
<label for="type">Type</label>
<select name="type" class="form-control" id="type"
name="type" required>
<option value=" ">Select Type</option>
<option value="accessories ">Best Sellers </option>
<option value="clothing ">New Arrivals</option>
<option value="shoes ">Sale</option>
</select>
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form> --}}
{{-- <form class="needs-validation" action="{{ route('addNewProduct') }}"
method="POST" enctype="multipart/form-data" novalidate>
@csrf
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name"
name="name">
<div class="invalid-feedback">Please enter your name.</div>
@error('name')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="file">Picture</label>
<input type="file" class="form-control" id="file" name="file"
required>
<div class="invalid-feedback">Please upload a picture.</div>
@error('file')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea class="form-control" id="description" name="description" required></textarea>
<div class="invalid-feedback">Please enter a description.</div>
@error('description')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="price">Price</label>
<input type="number" class="form-control" id="price" name="price"
required>
<div class="invalid-feedback">Please enter the price.</div>
@error('price')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="quantity">Quantity</label>
<input type="number" class="form-control" id="quantity"
name="quantity" required>
<div class="invalid-feedback">Please enter the quantity.</div>
@error('quantity')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="category">Category</label>
<select name="category" class="form-control" id="category"
name="category" required>
<option value=" ">Select Category</option>
<option value="accessories ">Accessories </option>
<option value="clothing ">Clothing </option>
<option value="shoes ">Shoes </option>
</select>
<div class="invalid-feedback">Please select a category.</div>
@error('category')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="type">Type</label>
<select name="type" class="form-control" id="type"
name="type" required>
<option value=" ">Select Type</option>
<option value="accessories ">Best Sellers </option>
<option value="clothing ">New Arrivals</option>
<option value="shoes ">Sale</option>
</select>
<div class="invalid-feedback">Please select a type.</div>
@error('type')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form> --}}
<form class="needs-validation" action="{{ route('addNewProduct') }}"
method="POST" enctype="multipart/form-data" novalidate>
@csrf
@ -290,7 +148,7 @@
<p class="card-title mb-0">Top Products</p>
<div class="table-responsive">
<table class="table table-striped table-borderless">
<table class="table table-striped table-bordered" id="product-table">
<thead>
<tr>
<th>ID</th>
@ -306,26 +164,173 @@
</tr>
</thead>
<tbody>
@php
$i = 0;
@endphp
@foreach ($products as $item)
@php
$i++;
@endphp
<tr>
<td>{{ $item->id }}</td>
<td>{{ $item->name }}</td>
<td><img src="{{ URL::asset('uploads/products/' . $item->picture) }}"
alt="" width="100px"></td>
{{-- <td> <img src="{{ URL::asset('uploads/products/' . $item->picture) }}"
alt="" width="100px"> </td> --}}
{{-- <td>{{ $item->description }}</td> --}}
<td>Nrs.{{ $item->price }}</td>
<td>{{ $item->quantity }}</td>
<td>{{ $item->category }}</td>
<td>{{ $item->type }}</td>
<td>
{{-- update --}}
<!-- Button to Open the Modal -->
<button type="button" class="btn btn-success" data-toggle="modal"
data-target="#updateModal{{ $i }}">
Edit
</button>
<!-- The Modal -->
<div class="modal" id="updateModal{{ $i }}">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title"> Edit Product</h4>
<button type="button" class="close"
data-dismiss="modal">&times;</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<form class="needs-validation"
action="{{ route('updateProduct') }}"
method="POST" enctype="multipart/form-data"
novalidate>
@csrf
<div class="form-group">
<label for="name">Name</label>
<input type="text"
value="{{ $item->name }}"
class="form-control" id="name"
name="name" required>
<div class="invalid-feedback">Please enter your
name.</div>
@error('name')
<div class="alert alert-danger">
{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="file">Picture</label>
<input type="file"
value="{{ $item->picture }}"
class="form-control" id="file"
name="file" required>
<div class="invalid-feedback">Please upload a
picture.</div>
@error('file')
<div class="alert alert-danger">
{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea class="form-control" value="{{ $item->description }}" id="description" name="description" required>{{ $item->description }}</textarea>
<div class="invalid-feedback">Please enter
description.</div>
@error('description')
<div class="alert alert-danger">
{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="price">Price</label>
<input type="number" class="form-control"
value="{{ $item->price }}"
id="price" name="price" required>
<div class="invalid-feedback">Please enter the
price.</div>
@error('price')
<div class="alert alert-danger">
{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="quantity">Quantity</label>
<input type="number" class="form-control"
value="{{ $item->quantity }}"
id="quantity" name="quantity" required>
<div class="invalid-feedback">Please enter the
quantity.</div>
@error('quantity')
<div class="alert alert-danger">
{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="category">Category</label>
<select class="form-control" id="category"
name="category" required>
<option value=" {{ $item->category }}">
{{ $item->category }}
</option>
<option value="accessories">Accessories
</option>
<option value="clothing">Clothing</option>
<option value="shoes">Shoes</option>
</select>
<div class="invalid-feedback">Please select a
category.</div>
@error('category')
<div class="alert alert-danger">
{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="type">Type</label>
<select class="form-control" id="type"
name="type" required>
<option value=" {{ $item->type }}">
{{ $item->type }}</option>
<option value="best-sellers">Best Sellers
</option>
<option value="new-arrivals">New Arrivals
</option>
<option value="sale">Sale</option>
</select>
<div class="invalid-feedback">Please select a
type.</div>
@error('type')
<div class="alert alert-danger">
{{ $message }}</div>
@enderror
</div>
<input type="hidden" name="id"
id="id" value="{{ $item->id }}">
<button type="submit"
class="btn btn-success">Save Changes</button>
</form>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger"
data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<a href="{{ route('deleteProduct', $item->id) }}"
class="btn btn-danger">Delete</a>
</td>
@endforeach
</tr>
</tbody>
</table>
{{-- <div class="d-flex justify-content-center mt-3">
{{ $products->links() }}
</div> --}}
</div>
</div>
</div>

View File

@ -41,6 +41,16 @@
<script src="Dashboard/js/dashboard.js"></script>
<script src="Dashboard/js/Chart.roundedBarCharts.js"></script>
<!-- End custom js for this page-->
<!-- datatables-->
<script src="Dashboard/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.table').DataTable();
});
</script>
<!-- end datatables-->
</body>
{{-- <script>

View File

@ -30,6 +30,19 @@
<link rel="stylesheet" href="Dashboard/css/vertical-layout-light/style.css">
<!-- endinject -->
<link rel="shortcut icon" href="Dashboard/images/favicon.png" />
<!-- datatables -->
<!-- DataTables CSS -->
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.4/css/dataTables.bootstrap4.min.css">
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<!-- DataTables JS -->
<script type="text/javascript" src="https://cdn.datatables.net/1.11.4/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.11.4/js/dataTables.bootstrap4.min.js"></script>
</head>
<body>

View File

@ -13,6 +13,8 @@ use Illuminate\Support\Facades\Route;
Route::get('/admin', [AdminController::class, 'index'])->name('admin');
Route::get('/adminProducts', [AdminController::class, 'products'])->name('products');
Route::post('/addNewProduct', [AdminController::class, 'addNewProduct'])->name('addNewProduct');
Route::post('/updateProduct', [AdminController::class, 'updateProduct'])->name('updateProduct');
Route::get('/deleteProduct/{id}', [AdminController::class, 'deleteProduct'])->name('deleteProduct');