"Updated AdminController, added products method and addNewProduct method, modified adminfooter and adminheader blade files, and added routes for admin products and add new product"

This commit is contained in:
UronShrestha 2024-07-14 10:22:06 +05:45
parent 1550ab5d30
commit f37e18f125
12 changed files with 486 additions and 53 deletions

View File

@ -3,11 +3,78 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\Product;
class AdminController extends Controller class AdminController extends Controller
{ {
public function admin() public function index()
{ {
return view('Dashboard.index'); return view('Dashboard.index');
} }
public function products()
{
$products = Product::all();
return view('Dashboard.products', compact('products'));
}
// public function addNewProduct()
// {
// $validated = request()->validate([
// 'name' => 'required',
// 'price' => 'required',
// 'description' => 'required',
// 'image' => 'required|image|mimes:jpeg,png,jpg,gif,
// svg|max:2048',
// 'quantity' => 'required',
// ]);
// if (request()->hasFile('file')) {
// $file = request()->file('file');
// $fileName = $file->getClientOriginalName();
// $file->move('uploads/products/', $fileName);
// request()->merge([
// 'picture' => $fileName,
// ]);
// }
// Product::create(request()->all());
// // $product = new Product();
// // $product->name = $data->input('name');
// // $product->picture = $data->file('file')->getClientOriginalName();
// // $data->file('file')->move('uploads/products/', $product->picture);
// // $product->description = $data->input('description');
// // $product->price = $data->input('price');
// // $product->quantity = $data->input('quantity');
// // $product->category = $data->input('category');
// // $product->type = $data->input('type');
// // $abc = $product->save();
// // dd($abc);
// return redirect()->back()->with('success', 'Product Added Successfully');
// // return view('Dashboard.addNewProduct');
// }
public function addNewProduct(Request $request)
{
$validated = $request->validate([
'name' => 'required',
'price' => 'required|numeric',
'description' => 'required',
'file' => 'required|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::create($validated);
return redirect()->back()->with('success', 'Product Added Successfully');
}
} }

20
app/Models/Product.php Normal file
View File

@ -0,0 +1,20 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
protected $fillable = [
'name',
'description',
'price',
'picture',
'category',
'type',
'quantity',
];
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

View File

@ -0,0 +1,339 @@
<x-adminheader />
<!-- partial -->
<div class="main-panel">
<div class="content-wrapper">
<div class="row">
<div class="col-md-12 grid-margin">
<div class="row">
<div class="col-12 col-xl-8 mb-4 mb-xl-0">
<h3 class="font-weight-bold">Welcome Aamir</h3>
<h6 class="font-weight-normal mb-0">All systems are running smoothly! You have
<span class="text-primary">3 unread alerts!</span>
</h6>
</div>
<div class="col-12 col-xl-4">
<div class="justify-content-end d-flex">
<div class="dropdown flex-md-grow-1 flex-xl-grow-0">
<button class="btn btn-sm btn-light bg-white dropdown-toggle" type="button"
id="dropdownMenuDate2" data-toggle="dropdown" aria-haspopup="true"
aria-expanded="true">
<i class="mdi mdi-calendar"></i> Today (10 Jan 2021)
</button>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuDate2">
<a class="dropdown-item" href="#">January - March</a>
<a class="dropdown-item" href="#">March - June</a>
<a class="dropdown-item" href="#">June - August</a>
<a class="dropdown-item" href="#">August - November</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 grid-margin stretch-card">
<div class="card">
<div class="card-body">
<!-- Button to Open the Modal -->
<button type="button" class="btn btn-primary float-right" data-toggle="modal"
data-target="#myModal">
Add New
</button>
<!-- The Modal -->
<div class="modal" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title"> Add New 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('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
<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>
@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 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 class="form-control" id="type" name="type" required>
<option value="">Select 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>
<button type="submit" class="btn btn-primary">Submit</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>
<p class="card-title mb-0">Top Products</p>
<div class="table-responsive">
<table class="table table-striped table-borderless">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Picture</th>
{{-- <th>Description</th> --}}
<th>Price</th>
<th>Quantity</th>
<th>Category</th>
<th>Type</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach ($products as $item)
<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>
@endforeach
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- content-wrapper ends -->
<x-adminfooter />

View File

@ -43,4 +43,47 @@
<!-- End custom js for this page--> <!-- End custom js for this page-->
</body> </body>
{{-- <script>
$(document).ready(function() {
// Disable form submissions if there are invalid fields
$('form.needs-validation').on('submit', function(event) {
event.preventDefault(); // Prevent the default form submission
var form = $(this);
if (form[0].checkValidity() === false) {
event.stopPropagation();
form.addClass('was-validated');
} else {
// If the form is valid, submit it via AJAX
$.ajax({
type: form.attr('method'), // GET or POST
url: form.attr('action'), // The form action URL
data: form.serialize(), // Serialize the form data
success: function(response) {
// Handle the successful form submission
Swal.fire({
icon: 'success',
title: 'Success',
text: 'Form submitted successfully!',
});
console.log(response);
// You can update the page content or redirect here
},
error: function(xhr, status, error) {
// Handle the error response
Swal.fire({
icon: 'error',
title: 'Error',
text: 'Form submission failed. Please try again.',
});
console.error(xhr.responseText);
}
});
}
form.addClass('was-validated');
});
});
</script> --}}
</html> </html>

View File

@ -2,6 +2,16 @@
<html lang="en"> <html lang="en">
<head> <head>
{{-- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script> --}}
<!-- Required meta tags --> <!-- Required meta tags -->
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
@ -27,7 +37,7 @@
<!-- partial:partials/_navbar.html --> <!-- partial:partials/_navbar.html -->
<nav class="navbar col-lg-12 col-12 p-0 fixed-top d-flex flex-row"> <nav class="navbar col-lg-12 col-12 p-0 fixed-top d-flex flex-row">
<div class="text-center navbar-brand-wrapper d-flex align-items-center justify-content-center"> <div class="text-center navbar-brand-wrapper d-flex align-items-center justify-content-center">
<a class="navbar-brand brand-logo mr-5" href="index.html"><img src="Dashboard/images/logo.svg" <a class="navbar-brand brand-logo mr-5" href="{{ route('admin') }}"><img src="Dashboard/images/logo.svg"
class="mr-2" alt="logo" /></a> class="mr-2" alt="logo" /></a>
<a class="navbar-brand brand-logo-mini" href="index.html"><img src="Dashboard/images/logo-mini.svg" <a class="navbar-brand brand-logo-mini" href="index.html"><img src="Dashboard/images/logo-mini.svg"
alt="logo" /></a> alt="logo" /></a>
@ -336,7 +346,7 @@
</a> </a>
<div class="collapse" id="ui-basic"> <div class="collapse" id="ui-basic">
<ul class="nav flex-column sub-menu"> <ul class="nav flex-column sub-menu">
<li class="nav-item"> <a class="nav-link" href="#">View <li class="nav-item"> <a class="nav-link" href="{{ route('products') }}">View
All</a> All</a>
</li> </li>
@ -371,34 +381,7 @@
</ul> </ul>
</div> </div>
</li> </li>
{{-- <li class="nav-item">
<a class="nav-link" data-toggle="collapse" href="#tables" aria-expanded="false"
aria-controls="tables">
<i class="icon-grid-2 menu-icon"></i>
<span class="menu-title">Tables</span>
<i class="menu-arrow"></i>
</a>
<div class="collapse" id="tables">
<ul class="nav flex-column sub-menu">
<li class="nav-item"> <a class="nav-link" href="pages/tables/basic-table.html">Basic
table</a></li>
</ul>
</div>
</li> --}}
{{-- <li class="nav-item">
<a class="nav-link" data-toggle="collapse" href="#icons" aria-expanded="false"
aria-controls="icons">
<i class="icon-contract menu-icon"></i>
<span class="menu-title">Icons</span>
<i class="menu-arrow"></i>
</a>
<div class="collapse" id="icons">
<ul class="nav flex-column sub-menu">
<li class="nav-item"> <a class="nav-link" href="pages/icons/mdi.html">Mdi icons</a>
</li>
</ul>
</div>
</li> --}}
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" data-toggle="collapse" href="#auth" aria-expanded="false" <a class="nav-link" data-toggle="collapse" href="#auth" aria-expanded="false"
aria-controls="auth"> aria-controls="auth">
@ -415,27 +398,6 @@
</ul> </ul>
</div> </div>
</li> </li>
{{-- <li class="nav-item">
<a class="nav-link" data-toggle="collapse" href="#error" aria-expanded="false"
aria-controls="error">
<i class="icon-ban menu-icon"></i>
<span class="menu-title">Error pages</span>
<i class="menu-arrow"></i>
</a>
<div class="collapse" id="error">
<ul class="nav flex-column sub-menu">
<li class="nav-item"> <a class="nav-link" href="pages/samples/error-404.html"> 404
</a></li>
<li class="nav-item"> <a class="nav-link" href="pages/samples/error-500.html"> 500
</a></li>
</ul>
</div>
</li> --}}
{{-- <li class="nav-item">
<a class="nav-link" href="pages/documentation/documentation.html">
<i class="icon-paper menu-icon"></i>
<span class="menu-title">Documentation</span>
</a>
</li> --}}
</ul> </ul>
</nav> </nav>

View File

@ -11,6 +11,8 @@ use Illuminate\Support\Facades\Route;
//admin routes //admin routes
Route::get('/admin', [AdminController::class, 'index'])->name('admin'); Route::get('/admin', [AdminController::class, 'index'])->name('admin');
Route::get('/adminProducts', [AdminController::class, 'products'])->name('products');
Route::post('/addNewProduct', [AdminController::class, 'addNewProduct'])->name('addNewProduct');