2024-07-12 07:06:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2024-07-15 08:06:36 +00:00
|
|
|
use Illuminate\Support\Facades\URL;
|
2024-07-14 11:00:49 +00:00
|
|
|
use App\Models\User;
|
|
|
|
|
2024-07-15 08:06:36 +00:00
|
|
|
|
2024-07-12 07:06:02 +00:00
|
|
|
use Illuminate\Http\Request;
|
2024-07-14 04:37:06 +00:00
|
|
|
use App\Models\Product;
|
|
|
|
|
2024-07-12 07:06:02 +00:00
|
|
|
|
|
|
|
class AdminController extends Controller
|
|
|
|
{
|
2024-07-14 04:37:06 +00:00
|
|
|
public function index()
|
2024-07-12 07:06:02 +00:00
|
|
|
{
|
2024-07-14 09:31:39 +00:00
|
|
|
if (session()->get('type') == 'Admin') {
|
|
|
|
return view('Dashboard.index');
|
|
|
|
}
|
|
|
|
return redirect()->back();
|
2024-07-12 07:06:02 +00:00
|
|
|
}
|
2024-07-14 11:00:49 +00:00
|
|
|
|
2024-07-14 04:37:06 +00:00
|
|
|
public function products()
|
|
|
|
{
|
2024-07-14 09:31:39 +00:00
|
|
|
if (session()->get('type') == 'Admin') {
|
|
|
|
$products = Product::all();
|
|
|
|
return view('Dashboard.products', compact('products'));
|
|
|
|
}
|
|
|
|
return redirect()->back();
|
2024-07-14 04:37:06 +00:00
|
|
|
}
|
|
|
|
// 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');
|
|
|
|
// }
|
2024-07-14 06:40:49 +00:00
|
|
|
|
|
|
|
//mass assignment for adding new product
|
2024-07-14 04:37:06 +00:00
|
|
|
public function addNewProduct(Request $request)
|
|
|
|
{
|
2024-07-14 09:31:39 +00:00
|
|
|
if (session()->get('type') == 'Admin') {
|
|
|
|
$validated = $request->validate([
|
|
|
|
'name' => 'required',
|
|
|
|
'price' => 'required|numeric',
|
|
|
|
'description' => 'required',
|
2024-07-14 11:00:49 +00:00
|
|
|
'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:10240',
|
2024-07-14 09:31:39 +00:00
|
|
|
'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');
|
2024-07-14 04:37:06 +00:00
|
|
|
}
|
2024-07-14 09:31:39 +00:00
|
|
|
return redirect()->back();
|
2024-07-14 04:37:06 +00:00
|
|
|
}
|
2024-07-14 06:40:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
//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)
|
|
|
|
{
|
2024-07-14 09:31:39 +00:00
|
|
|
if (session()->get('type') == 'Admin') {
|
|
|
|
$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');
|
2024-07-14 06:40:49 +00:00
|
|
|
}
|
2024-07-14 09:31:39 +00:00
|
|
|
return redirect()->back();
|
2024-07-14 06:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteProduct($id)
|
|
|
|
{
|
2024-07-14 09:31:39 +00:00
|
|
|
if (session()->get('type') == 'Admin') {
|
|
|
|
$product = Product::find($id);
|
|
|
|
$product->delete();
|
|
|
|
return redirect()->back()->with('success', 'Product Deleted Successfully');
|
|
|
|
}
|
|
|
|
return redirect()->back();
|
2024-07-14 06:40:49 +00:00
|
|
|
}
|
2024-07-14 11:00:49 +00:00
|
|
|
|
|
|
|
public function profile()
|
|
|
|
{
|
|
|
|
if (session()->get('type') == 'Admin') {
|
|
|
|
$user = User::find(session()->get('id'));
|
|
|
|
return view('Dashboard.profile', compact('user'));
|
|
|
|
}
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
2024-07-15 08:06:36 +00:00
|
|
|
|
|
|
|
public function customers()
|
|
|
|
{
|
|
|
|
if (session()->get('type') == 'Admin') {
|
|
|
|
$customers = User::where('type', 'customer')->get();
|
|
|
|
return view('Dashboard.customers', compact('customers'));
|
|
|
|
}
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function changeUserStatus($status, $id)
|
|
|
|
{
|
|
|
|
if (session()->get('type') == 'Admin') {
|
|
|
|
$user = User::find($id);
|
|
|
|
$user->status = $status;
|
|
|
|
$user->save();
|
|
|
|
return redirect()->back()->with('success', 'User Status Changed Successfully');
|
|
|
|
}
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
2024-07-12 07:06:02 +00:00
|
|
|
}
|