2024-07-04 04:34:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2024-07-10 08:01:42 +00:00
|
|
|
use App\Mail\Testing;
|
2024-07-04 04:34:45 +00:00
|
|
|
use App\Models\User;
|
2024-07-05 06:55:14 +00:00
|
|
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
2024-07-04 12:07:47 +00:00
|
|
|
use App\Models\Cart;
|
2024-07-04 04:34:45 +00:00
|
|
|
use App\Models\Products;
|
2024-07-07 07:21:31 +00:00
|
|
|
use App\Models\Order;
|
|
|
|
use App\Models\OrderItem;
|
2024-07-04 04:34:45 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
2024-07-10 08:01:42 +00:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
2024-07-04 04:34:45 +00:00
|
|
|
|
|
|
|
class MainController extends Controller
|
|
|
|
{
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$allProducts = Products::all();
|
|
|
|
//dd($allProducts);
|
|
|
|
$newArrival = Products::where('type', 'new-arrival')->get();
|
|
|
|
$hotSale = Products::where('type', 'sale')->get();
|
|
|
|
|
|
|
|
|
|
|
|
return view('index', compact('allProducts', 'hotSale', 'newArrival'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function about()
|
|
|
|
{
|
|
|
|
return view('about');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function register()
|
|
|
|
{
|
|
|
|
return view('register');
|
|
|
|
}
|
|
|
|
|
2024-07-05 06:55:14 +00:00
|
|
|
|
2024-07-04 04:34:45 +00:00
|
|
|
|
|
|
|
public function registerUser(Request $data)
|
|
|
|
{
|
2024-07-08 09:42:39 +00:00
|
|
|
|
|
|
|
// dd($data->all());
|
2024-07-04 04:34:45 +00:00
|
|
|
// Validate the input data
|
|
|
|
$data->validate([
|
|
|
|
'name' => 'required|string|max:255',
|
|
|
|
'email' => 'required|string|email|max:255|unique:users',
|
|
|
|
'password' => 'required|string|min:8|confirmed',
|
|
|
|
'file' => 'required|file|mimes:jpg,png,jpeg|max:2048',
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Create a new user
|
|
|
|
try {
|
|
|
|
$newUser = new User();
|
|
|
|
$newUser->name = $data->input('name');
|
|
|
|
$newUser->email = $data->input('email');
|
|
|
|
$newUser->password = Hash::make($data->input('password')); // Hashing the password
|
|
|
|
$newUser->picture = $data->file('file')->getClientOriginalName();
|
|
|
|
$data->file('file')->move('uploads/profiles/', $newUser->picture);
|
|
|
|
$newUser->type = "Customer";
|
|
|
|
|
|
|
|
if ($newUser->save()) {
|
|
|
|
return redirect('login')->with('success', 'Account created successfully!');
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect('register')->with('error', 'Account creation failed. Please try again.');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return redirect('register')->with('error', 'An error occurred: ' . $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function login()
|
|
|
|
{
|
|
|
|
return view('login');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function loginUser(Request $data)
|
|
|
|
{
|
|
|
|
$user = User::where('email', $data->input('email'))->first();
|
|
|
|
if ($user && Hash::check($data->input('password'), $user->password)) { // Verifying the hashed password
|
|
|
|
session()->put('id', $user->id);
|
|
|
|
session()->put('type', $user->type);
|
|
|
|
if ($user->type == 'Customer') {
|
|
|
|
return redirect('/');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return redirect('login')->with('error', 'Invalid email or password!');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function logout()
|
|
|
|
{
|
|
|
|
session()->forget('id');
|
|
|
|
session()->forget('type');
|
|
|
|
return redirect('/login');
|
|
|
|
}
|
|
|
|
|
2024-07-04 07:19:28 +00:00
|
|
|
public function singleProduct($id)
|
2024-07-04 04:34:45 +00:00
|
|
|
{
|
2024-07-04 07:19:28 +00:00
|
|
|
$products = Products::find($id);
|
|
|
|
|
|
|
|
if (!$products) {
|
|
|
|
abort(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('singleProduct', compact('products'));
|
2024-07-04 04:34:45 +00:00
|
|
|
}
|
|
|
|
|
2024-07-04 07:19:28 +00:00
|
|
|
|
2024-07-04 04:34:45 +00:00
|
|
|
public function blogDetails()
|
|
|
|
{
|
|
|
|
return view('blogDetails');
|
|
|
|
}
|
|
|
|
|
2024-07-05 06:55:14 +00:00
|
|
|
// public function cart()
|
|
|
|
// {
|
|
|
|
// $cartItems = DB::table('products')
|
|
|
|
// ->join('carts', 'carts.productsId', 'product.id')
|
|
|
|
// ->select('products.name', 'products.price', 'products.picture', 'products.quantity as pQuantity', 'carts.*')
|
|
|
|
// ->where('carts.customerId', session()->get('id'))
|
|
|
|
// ->get();
|
|
|
|
|
|
|
|
// dd($cartItems);
|
|
|
|
|
|
|
|
// return view('cart', compact('cartItems'));
|
|
|
|
// }
|
|
|
|
|
|
|
|
public function cart()
|
|
|
|
{
|
|
|
|
$cartItems = DB::table('products')
|
2024-07-05 07:29:27 +00:00
|
|
|
// ->join('carts', 'carts.productId', '=', 'products.id')
|
|
|
|
->join('carts', 'carts.productId', 'products.id')
|
2024-07-05 06:55:14 +00:00
|
|
|
->select('products.name', 'products.price', 'products.picture', 'products.quantity as pQuantity', 'carts.*')
|
|
|
|
->where('carts.customerId', session()->get('id'))
|
|
|
|
->get();
|
|
|
|
|
|
|
|
//dd($cartItems);
|
|
|
|
|
|
|
|
return view('cart', compact('cartItems'));
|
|
|
|
}
|
|
|
|
|
2024-07-04 12:07:47 +00:00
|
|
|
public function addToCart(Request $data)
|
2024-07-04 04:34:45 +00:00
|
|
|
{
|
2024-07-04 12:07:47 +00:00
|
|
|
if (session()->has('id')) {
|
|
|
|
$item = new Cart();
|
|
|
|
$item->quantity = $data->input('quantity');
|
|
|
|
$item->productId = $data->input('id');
|
|
|
|
$item->customerId = session()->get('id');
|
|
|
|
|
|
|
|
$item->save();
|
|
|
|
return redirect()->back()->with('success', 'Item added to cart successfully!');
|
|
|
|
} else {
|
|
|
|
return redirect('/login')->with('error', 'Please login to add item to cart!');
|
|
|
|
}
|
2024-07-04 04:34:45 +00:00
|
|
|
}
|
|
|
|
|
2024-07-05 07:29:27 +00:00
|
|
|
public function deleteCartItem($id)
|
|
|
|
{
|
|
|
|
$item = Cart::find($id);
|
|
|
|
$item->delete();
|
|
|
|
return redirect()->back()->with('success', 'Item deleted from cart successfully!');
|
|
|
|
}
|
|
|
|
|
2024-07-05 09:58:08 +00:00
|
|
|
|
2024-07-07 07:21:31 +00:00
|
|
|
public function updateCartItem(Request $data, $id)
|
2024-07-05 09:58:08 +00:00
|
|
|
{
|
|
|
|
// dd($request->all());
|
|
|
|
if (session()->has('id')) {
|
|
|
|
$item = Cart::find($id);
|
|
|
|
if ($item) {
|
2024-07-07 07:21:31 +00:00
|
|
|
$item->quantity = $data->input('quantity');
|
2024-07-05 09:58:08 +00:00
|
|
|
$item->save();
|
|
|
|
return redirect()->back()->with('success', 'Item updated successfully!');
|
|
|
|
} else {
|
|
|
|
return redirect()->back()->with('error', 'Item not found!');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return redirect('/login')->with('error', 'Please login to update item!');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-09 05:59:05 +00:00
|
|
|
|
|
|
|
public function myOrders()
|
|
|
|
{
|
2024-07-10 08:01:42 +00:00
|
|
|
if (session()->has('id')) {
|
|
|
|
$orders = Order::where('customerId', session()->get('id'))->get();
|
|
|
|
$items = DB::table('products')
|
2024-07-09 05:59:05 +00:00
|
|
|
|
|
|
|
|
2024-07-10 08:01:42 +00:00
|
|
|
->join('order_items', 'order_items.productId', '=', 'products.id')
|
|
|
|
->select('products.name', 'products.picture', 'products.*')
|
|
|
|
->get();
|
2024-07-09 05:59:05 +00:00
|
|
|
|
2024-07-10 08:01:42 +00:00
|
|
|
return view('orders', compact('orders', 'items'));
|
|
|
|
}
|
2024-07-09 05:59:05 +00:00
|
|
|
|
2024-07-10 08:01:42 +00:00
|
|
|
return view('login');
|
|
|
|
}
|
2024-07-09 05:59:05 +00:00
|
|
|
|
|
|
|
|
2024-07-08 09:42:39 +00:00
|
|
|
public function profile()
|
|
|
|
{
|
|
|
|
if (session()->has('id')) {
|
|
|
|
$user = User::find(session()->get('id'));
|
|
|
|
return view('profile', compact('user'));
|
|
|
|
}
|
|
|
|
return redirect('login');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function updateUser(Request $data)
|
|
|
|
{
|
|
|
|
$user = User::find(session()->get('id'));
|
|
|
|
$user->name = $data->input('name');
|
|
|
|
$user->email = $data->input('email');
|
|
|
|
$user->password = $data->input('password');
|
|
|
|
|
|
|
|
if ($data->file('file') != null) {
|
|
|
|
$user->picture = $data->file('file')->getClientOriginalName();
|
|
|
|
$data->file('file')->move('uploads/profiles/', $user->picture);
|
|
|
|
}
|
|
|
|
if ($user->save()) {
|
|
|
|
return redirect()->back()->with('success', 'User updated successfully!');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-05 09:58:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-07-05 07:29:27 +00:00
|
|
|
|
|
|
|
|
2024-07-07 07:21:31 +00:00
|
|
|
public function checkout(Request $data)
|
2024-07-04 04:34:45 +00:00
|
|
|
{
|
2024-07-07 07:21:31 +00:00
|
|
|
if (session()->has('id')) {
|
|
|
|
$order = new Order();
|
|
|
|
$order->status = "Pending";
|
|
|
|
$order->customerId = session()->get('id');
|
|
|
|
$order->name = $data->input('name');
|
|
|
|
$order->phone = $data->input('phone');
|
|
|
|
$order->address = $data->input('address');
|
|
|
|
$order->bill = $data->input('bill');
|
|
|
|
if ($order->save()) {
|
|
|
|
$cartItems = Cart::where('customerId', session()->get('id'))->get();
|
|
|
|
foreach ($cartItems as $item) {
|
|
|
|
$product = Products::find($item->productId);
|
|
|
|
$orderItem = new OrderItem();
|
|
|
|
$orderItem->orderId = $order->id;
|
|
|
|
$orderItem->productId = $item->productId;
|
|
|
|
$orderItem->quantity = $item->quantity;
|
|
|
|
$orderItem->price = $product->price;
|
|
|
|
|
|
|
|
$orderItem->save();
|
|
|
|
$item->delete();
|
|
|
|
}
|
|
|
|
return redirect()->back()->with('success', 'Order placed successfully!');
|
|
|
|
} else {
|
|
|
|
return redirect('login')->back()->with('error', 'Order not placed!');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-04 04:34:45 +00:00
|
|
|
return view('checkout');
|
|
|
|
}
|
|
|
|
|
2024-07-10 08:01:42 +00:00
|
|
|
public function testMail()
|
|
|
|
{
|
|
|
|
$details = [
|
|
|
|
'title' => 'Mail from Uron Shrestha',
|
|
|
|
'message' => 'This is for testing mail using smtp in Laravel!'
|
|
|
|
];
|
|
|
|
Mail::to("yuron.stha57@gmail.com")->send(new Testing($details));
|
|
|
|
return redirect('/');
|
|
|
|
}
|
2024-07-08 09:42:39 +00:00
|
|
|
|
|
|
|
|
2024-07-04 04:34:45 +00:00
|
|
|
public function shop()
|
|
|
|
{
|
|
|
|
return view('shop');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function shopDetails()
|
|
|
|
{
|
|
|
|
return view('shopDetails');
|
|
|
|
}
|
|
|
|
}
|