laravelEcomm/app/Http/Controllers/MainController.php

161 lines
4.5 KiB
PHP
Raw Normal View History

2024-07-04 04:34:45 +00:00
<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Models\Products;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
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');
}
// public function registerUser(Request $data)
// {
// // 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',
// ]);
// // Check if the email already exists
// if (User::where('email', $data->input('email'))->exists()) {
// return redirect('register')->with('error', 'Email already exists!');
// }
// // Create a new user
// $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 view('register')->with('error', 'Account creation failed. Please try again.');
// }
public function registerUser(Request $data)
{
// 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');
}
public function cart()
{
return view('cart');
}
public function checkout()
{
return view('checkout');
}
public function shop()
{
return view('shop');
}
public function shopDetails()
{
return view('shopDetails');
}
}