ARTICLE AD BOX
@extends('layouts.app')
@section('title', 'Cart')
@section('content')
@if(session('success'))
<div class="alert alert-success alert-dismissible fade show mb-4" role="alert">
<strong>Success!</strong> {{ session('success') }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif
@if (empty($cart))
<div class="alert alert-info">
Cart empty
</div>
@else
<div class="col">
<a href="{{ route('customer.home') }}" class="btn btn-primary">Back</a>
</div>
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Price</th>
<th scope="col">Quantity</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
@foreach($cart as $id=>$item)
<tr>
<td>{{ $item['name'] }}</td>
<td>Rp {{ number_format($item['price']) }}</td>
<td>
<form method="POST" action="/update/{{ $id }}">
@csrf
<input type="number" name="qty" value="{{ $item['qty'] }}" min="1">
<button class="btn btn-sm btn-success">Update</button>
</form>
</td>
<td>
<a href="/delete/{{ $id }}" class="btn btn-danger btn-sm">Delete</a>
</td>
</tr>
@endforeach
</tbody>
</table>
<h4>Grand Total: Rp {{ number_format($total) }}</h4>
<form method="POST" action="/checkout">
@csrf
<button class="btn btn-primary">Checkout</button>
</form>
@endif
@endsection
this are the other practice idk is it right or not haha
<nav class="navbar navbar-expand-lg navbar-light bg-light"> <div class="container-fluid px-5"> <a class="navbar-brand" href="#" style="color:blue">Sunib</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <!-- Navbar Content --> <ul class="navbar-nav ms-auto"> <div class="dropdown"> <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false"> Lang </button> <ul class="dropdown-menu"> <li class="nav-item"> <a class="nav-link" href="/lang/en">EN</a> </li> <li class="nav-item"> <a class="nav-link" href="/lang/id">ID</a> </li> </ul> </div> @auth @if(auth()->user()->role == 'customer') <li class="nav-item"> <a class="nav-link" href="{{ route('customer.home') }}">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="/cart">{{ __('app.cart') }}</a> </li> <li class="nav-item"> <a class="nav-link" href="/transaction">{{ __('app.transaction') }}</a> </li> @else <li class="nav-item"> <a class="nav-link" href="/createForm">Create Product</a> </li> <li class="nav-item"> <a class="nav-link" href={{ route('admin.transactions') }}>View Transactions</a> </li> @endif <li class="nav-item"> <form method="POST" action="{{ route('logout') }}"> @csrf <button class="btn btn-danger btn-sm">Logout</button> </form> </li> @endauth </div> </div> </nav> @extends('layouts.app') @section('title', 'Admin Dashboard') @section('content') <div class="container text-center"> @if(session('success')) <div class="alert alert-success alert-dismissible fade show mb-4" role="alert"> <strong>Success!</strong> {{ session('success') }} <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> </div> @endif <h3>{{ __('app.welcome') }}, {{ Auth::user()->name }}!</h3> <div class="row"> <table class="table table-striped table-hover"> <thead class="table-dark"> <tr> <th scope="col">Category</th> <th scope="col">Name</th> <th scope="col">Price</th> <th scope="col">Action</th> </tr> </thead> <tbody> @forelse ($products as $product) <tr> <td>{{ $product->Category->category_name ?? 'Uncategorized' }}</td> <td>{{ $product->product_name }}</td> <td>Rp {{ number_format($product->product_price) }}</td> <td> <a href="{{ route('admin.edit', $product->id) }}" class="btn btn-warning btn-sm">Edit</a> <a href="{{ route('admin.delete', $product->id) }}" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure?')">Delete</a> </td> </tr> @empty <tr> <td colspan="4" class="text-center text-muted py-4"> There are no products yet. </td> </tr> @endforelse </tbody> </table> </div> </div> <div class="d-flex justify-content-center mt-4"> {{ $products->links() }} </div> @endsection @extends('layouts.app') @section('title', 'Create Product') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <h2 class="mb-4 text-center">Add New Product</h2> {{-- Display Validation Errors --}} @if ($errors->any()) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif {{-- FORM START --}} {{-- enctype="multipart/form-data" is REQUIRED for file uploads --}} <form action="{{ route('admin.store') }}" method="POST" enctype="multipart/form-data"> @csrf {{-- 1. Product Name --}} <div class="mb-3"> <label for="nameInput" class="form-label">Product Name</label> <input type="text" name="product_name" class="form-control" id="nameInput" placeholder="Enter product name" value="{{ old('product_name') }}"> </div> {{-- 2. Category Dropdown --}} <div class="mb-3"> <label for="categoryInput" class="form-label">Category</label> <select class="form-select" name="category_id" id="categoryInput"> <option selected disabled>Choose a category...</option> @foreach($categories as $category) <option value="{{ $category->id }}">{{ $category->category_name }}</option> @endforeach </select> </div> {{-- 3. Price --}} <div class="mb-3"> <label for="priceInput" class="form-label">Price (IDR)</label> <input type="number" name="product_price" class="form-control" id="priceInput" placeholder="15000" value="{{ old('product_price') }}"> </div> {{-- 4. Description --}} <div class="mb-3"> <label for="descInput" class="form-label">Description</label> <textarea class="form-control" name="product_desc" id="descInput" rows="3">{{ old('product_desc') }}</textarea> </div> {{-- 5. Photo --}} <div class="mb-3"> <label for="photoInput" class="form-label">Product Photo</label> <input class="form-control" type="file" name="product_photo" id="photoInput"> </div> {{-- Submit Button --}} <div class="d-grid gap-2"> <button type="submit" class="btn btn-primary">Save Product</button> <a href="{{ route('admin.home') }}" class="btn btn-secondary">Cancel</a> </div> </form> {{-- FORM END --}} </div> </div> </div> @endsection @extends('layouts.app') @section('title', 'All Transactions') @section('content') <div class="container"> <div class="d-flex justify-content-between align-items-center mb-4"> <h2>All Transactions</h2> <a href="{{ route('admin.home') }}" class="btn btn-secondary">Back to Dashboard</a> </div> <div class="card shadow-sm"> <div class="card-body p-0"> <table class="table table-striped mb-0"> <thead class="table-dark"> <tr> <th scope="col">ID</th> <th scope="col">User</th> <th scope="col">Products Purchased</th> <th scope="col">Total Price</th> <th scope="col">Date</th> </tr> </thead> <tbody> @forelse($transactions as $transaction) <tr> {{-- Transaction ID --}} <td>#{{ $transaction->id }}</td> {{-- User Name (Requires 'user' relationship) --}} <td> <strong>{{ $transaction->user->name ?? 'Unknown User' }}</strong> <br> <small class="text-muted">{{ $transaction->user->email ?? '' }}</small> </td> {{-- List of Items --}} <td> <ul class="list-unstyled mb-0"> @foreach($transaction->Product as $product) <li> • {{ $product->product_name }} <span class="badge bg-secondary text-light">x{{ $product->pivot->quantity }}</span> </li> @endforeach </ul> </td> {{-- Grand Total --}} <td> <strong>Rp {{ number_format($transaction->total_price) }}</strong> </td> {{-- Date --}} <td>{{ $transaction->created_at->format('d M Y') }}</td> </tr> @empty <tr> <td colspan="5" class="text-center py-4">No transactions found.</td> </tr> @endforelse </tbody> </table> </div> </div> {{-- Pagination Links --}} <div class="mt-4 d-flex justify-content-center"> {{ $transactions->links() }} </div> </div> @endsection @extends('layouts.app') @section('title', 'Detail') @section('content') <div class="container text-center"> <div class="row align-items-start"> <div class="col"> <a href="{{ route('customer.home') }}" class="btn btn-primary">Back</a> </div> <div class="col"> <div class="card" style="width: 18rem;"> <img src="{{ asset('storage/'.$product->product_photo) }}" class="card-img-top"> <div class="card-body"> <h5 class="card-title">{{ $product->product_name }}</h5> <h5 class="card-title">{{ $product->Category->category_name }}</h5> <p class="card-text">{{ __('app.price') }}: Rp {{ number_format($product->product_price) }}</p> <p class="card-text">{{ $product->product_desc }}</p> <form method="POST" action="/buy/{{ $product->id }}"> @csrf <button class="btn btn-primary">{{ __('app.buy') }}</button> </form> </div> </div> </div> </div> </div> @endsection @extends('layouts.app') @section('title', __('app.transaction')) @section('content') <div class="container"> <div class="row mb-4"> <div class="col"> <a href="{{ route('customer.home') }}" class="btn btn-primary">Back to Home</a> </div> </div> @if($transactions->isEmpty()) <div class="alert alert-info"> You have no transactions yet. </div> @else <div class="alert alert-primary"> You have made <strong>{{ $transactions->count() }}</strong> transactions. </div> @foreach($transactions as $transaction) <div class="card mb-4"> <div class="card-header bg-light"> <div class="d-flex justify-content-between align-items-center"> <div> <strong>Transaction ID:</strong> #{{ $transaction->id }} <br> <small class="text-muted">Date: {{ $transaction->created_at->format('d M Y, H:i') }}</small> </div> <div> <h5 class="mb-0">Grand Total: Rp {{ number_format($transaction->total_price) }}</h5> </div> </div> </div> <div class="card-body p-0"> <table class="table table-striped mb-0"> <thead class="table-dark"> <tr> <th scope="col">Product Name</th> <th scope="col">Category</th> <th scope="col">Price</th> <th scope="col">Qty</th> <th scope="col">Subtotal</th> </tr> </thead> <tbody> @foreach($transaction->Product as $product) <tr> <td>{{ $product->product_name }}</td> <td> <span class="badge bg-secondary"> {{ $product->Category->category_name ?? 'Uncategorized' }} </span> </td> <td>Rp {{ number_format($product->pivot->price) }}</td> <td>{{ $product->pivot->quantity }}</td> <td> <strong> Rp {{ number_format($product->pivot->price * $product->pivot->quantity) }} </strong> </td> </tr> @endforeach </tbody> </table> </div> </div> @endforeach @endif </div> @endsection <?php namespace App\Http\Controllers; use App\Models\Category; use App\Models\Product; use App\Models\Transaction; use App\Models\Cart; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; class ProductController extends Controller { public function home(Request $request) { $categories = Category::all(); $query = Product::with('Category'); if ($request->filled('search')) { $query->where('product_name', 'like', '%' . $request->search . '%'); } if ($request->filled('category')) { $query->where('category_id', $request->category); } $products = $query->paginate(3); return view('home', compact('products', 'categories')); } public function detail($id) { $product = Product::with('Category')->find($id); return view('detail', compact('product')); } public function buy(Product $product) { if (!Auth::check()) { return redirect()->route('login')->with('error', 'Please login first'); } $userId = Auth::id(); $cartItem = Cart::where('user_id', $userId) ->where('product_id', $product->id) ->first(); if ($cartItem) { $cartItem->quantity += 1; $cartItem->save(); } else { Cart::create([ 'user_id' => $userId, 'product_id' => $product->id, 'quantity' => 1 ]); } return redirect()->back()->with('success', 'Product added to cart'); } public function cart() { $cartItems = Cart::where('user_id', Auth::id())->with('product')->get(); $total = $cartItems->sum(function ($item) { return $item->product->product_price * $item->quantity; }); return view('cart', compact('cartItems', 'total')); } public function update(Request $request, $id) { $request->validate([ 'qty' => 'required|integer|min:1' ]); $cartItem = Cart::where('id', $id)->where('user_id', Auth::id())->first(); if ($cartItem) { $cartItem->update(['quantity' => $request->qty]); } return redirect()->back()->with('success', 'Cart updated'); } public function delete($id) { Cart::where('id', $id)->where('user_id', Auth::id())->delete(); return redirect()->back()->with('success', 'Item removed'); } public function checkout() { $cartItems = Cart::where('user_id', Auth::id())->with('product')->get(); if ($cartItems->isEmpty()) { return redirect()->back()->with('error', 'Cart is empty!'); } $total = $cartItems->sum(function ($item) { return $item->product->product_price * $item->quantity; }); $transaction = Transaction::create([ 'user_id' => Auth::id(), 'total_price' => $total, ]); foreach ($cartItems as $item) { $transaction->Product()->attach($item->product_id, [ 'quantity' => $item->quantity, 'price' => $item->product->product_price ]); } Cart::where('user_id', Auth::id())->delete(); return redirect()->back()->with('success', 'Checkout successful!'); } public function transaction(){ $transactions = Transaction::where('user_id', Auth::id()) ->with('Product.Category') ->orderBy('created_at', 'desc') ->get(); return view('transaction', compact('transactions')); } } <?php namespace App\Http\Controllers; use App\Models\Category; use App\Models\Product; use App\Models\Transaction; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; class ProductController extends Controller { /** * Display a listing of the resource. */ public function home(Request $request) { // // $products = Product::with('Category')->paginate(3); $categories = Category::all(); $query = Product::with('Category'); // 3. Filter by Search (Product Name) if ($request->filled('search')) { $search = $request->search; $query->where('product_name', 'like', "%$search%"); } // 4. Filter by Category if ($request->filled('category')) { $query->where('category_id', $request->category); } // 5. Paginate $products = $query->paginate(3); return view('home', compact( 'products', 'categories')); } public function detail($id) { // $product = Product::with('Category')->find($id); return view('detail', compact( 'product')); } public function buy(Product $product) { // $cart = session()->get('cart', []); if (isset($cart[$product->id])) { $cart[$product->id]['qty'] += 1; } else { $cart[$product->id] = [ 'name' => $product->product_name, 'price' => $product->product_price, 'qty' => 1 ]; } session()->put('cart', $cart); return redirect()->back()->with('success', 'Product added to cart'); } public function cart(){ $cart = session()->get('cart', []); $total = collect($cart)->sum(function ($item) { return $item['price'] * $item['qty']; }); return view('cart', compact('cart', 'total')); } public function update(Request $request, $id) { $request->validate([ 'qty' => 'required|integer|min:1' ]); $cart = session()->get('cart'); if (isset($cart[$id])) { $cart[$id]['qty'] = $request->qty; session()->put('cart', $cart); } return redirect()->back()->with('success', 'Cart updated'); } public function delete($id) { $cart = session()->get('cart'); if (isset($cart[$id])) { unset($cart[$id]); session()->put('cart', $cart); } return redirect()->back()->with('success', 'Item removed'); } public function checkout() { // session()->forget('cart'); $cart = session()->get('cart'); if (!$cart) { return redirect()->back()->with('error', 'Cart is empty!'); } $total = collect($cart)->sum(function ($item) { return $item['price'] * $item['qty']; }); $transaction = Transaction::create([ 'user_id' => Auth::id(), 'total_price' => $total, ]); //pivot table foreach ($cart as $productId => $prod) { $transaction->Product()->attach($productId, [ 'quantity' => $prod['qty'], 'price' => $prod['price'] ]); } session()->forget('cart'); return redirect()->back()->with('success', 'Cart Checkout!'); } public function transaction(){ $transactions = Transaction::where('user_id', Auth::id()) ->with('Product.Category') ->orderBy('created_at', 'desc') ->get(); return view('transaction', compact('transactions')); } } <?php namespace App\Http\Controllers; use App\Models\Category; use App\Models\Product; use App\Models\Transaction; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; class AdminController extends Controller { public function admin() { $products = Product::with('Category')->paginate(3); return view('admin', compact('products')); } public function createForm() { $categories = Category::all(); return view('createproduct', compact('categories')); } public function store(Request $request) { $request->validate([ 'product_name' => 'required|string', 'product_price' => 'required|numeric|min:0', 'product_desc' => 'required|string', 'category_id' => 'required', 'product_photo' => 'required' ]); $photoPath = $request->file('product_photo')->store('products', 'public'); Product::create([ 'product_name' => $request->product_name, 'product_price' => $request->product_price, 'product_desc' => $request->product_desc, 'category_id' => $request->category_id, 'product_photo' => $photoPath ]); return redirect()->route('admin.home')->with('success', 'Product created successfully!'); } public function edit($id) { $product = Product::find($id); $categories = Category::all(); return view('editproduct', compact('product', 'categories')); } public function update(Request $request, $id) { $product = Product::findOrFail($id); $request->validate([ 'product_name' => 'required|string|max:255', 'product_price' => 'required|numeric', 'product_desc' => 'required', 'category_id' => 'required|exists:categories,id', 'product_photo' => 'nullable|image|max:2048' ]); // Handle Photo Update if ($request->hasFile('product_photo')) { if ($product->product_photo && Storage::disk('public')->exists($product->product_photo)) { Storage::disk('public')->delete($product->product_photo); } // Store new photo $newPhoto = $request->file('product_photo')->store('products', 'public'); // Update the photo column $product->product_photo = $newPhoto; } // Update other fields $product->update([ 'product_name' => $request->product_name, 'product_price' => $request->product_price, 'product_desc' => $request->product_desc, 'category_id' => $request->category_id, ]); return redirect()->route('admin.home')->with('success', 'Product updated successfully!'); } public function delete($id) { $product = Product::findOrFail($id); // Delete photo from storage to save space if ($product->product_photo && Storage::disk('public')->exists($product->product_photo)) { Storage::disk('public')->delete($product->product_photo); } $product->delete(); return redirect()->back()->with('success', 'Product deleted successfully!'); } public function transactions() { $transactions = Transaction::with(['Product', 'User']) ->orderBy('created_at', 'desc') ->paginate(3); return view('admintransactions', compact('transactions')); } }