/
home
/
sjslayjy
/
public_html
/
theweavenest
/
app
/
Http
/
Controllers
/
Admin
/
Upload File
HOME
<?php namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Models\Admin\Category; use App\Models\Admin\Sub_User; use Illuminate\Http\Request; use App\Models\Admin\Product; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; class CategoryController extends Controller { public function index() { $data = Category::all(); return view('admin.category', compact('data')); } public function manage_category(Request $request, $id = '') { if ($id > 0) { $category = Category::findOrFail($id); $result['CategoryName'] = $category->CategoryName; $result['CategoryImage'] = $category->CategoryImage; $result['is_home'] = $category->is_home; $result['is_home_selected'] = $category->is_home == 1 ? "checked" : ""; $result['id'] = $category->id; $result['categories'] = Category::where('status', 1)->where('id', '!=', $id)->get(); } else { $result['CategoryName'] = ''; $result['CategoryImage'] = ''; $result['is_home'] = ''; $result['is_home_selected'] = ""; $result['id'] = 0; $result['categories'] = Category::where('status', 1)->get(); } return view('admin.manage_category', $result); } public function manage_category_process(Request $request) { $request->validate([ 'CategoryName' => 'required', // 'CategoryImage' => 'image|mimes:jpeg,png,jpg,gif|max:2048', 'is_home' => 'nullable|boolean', ]); $id = $request->input('id'); // Check if the category with the given ID exists $model = Category::find($id); // Determine the action: update or insert if ($model) { $msg = "Category updated"; } else { $model = new Category(); $msg = "Category inserted"; } // Set the category attributes $model->CategoryName = $request->input('CategoryName'); $model->is_home = $request->input('is_home') ? 1 : 0; // Handle category image if ($request->hasFile('CategoryImage')) { // Delete old image if updating if ($model->CategoryImage) { Storage::delete('/public/media/category/' . $model->CategoryImage); } // Store new image $image = $request->file('CategoryImage'); $ext = $image->extension(); $image_name = time() . '.' . $ext; $image->storeAs('/public/media/category', $image_name); $model->CategoryImage = $image_name; } // Save the category $model->save(); // Flash message and redirect $request->session()->flash('message', $msg); return redirect('admin/category'); } public function delete(Request $request, $id) { $category = Category::findOrFail($id); Storage::delete('public/media/category/' . $category->CategoryImage); $category->delete(); $request->session()->flash('message', 'Category deleted'); return redirect('admin/category'); } public function status(Request $request, $status, $id) { $category = Category::findOrFail($id); $category->status = $status; $category->save(); $request->session()->flash('message','Category status updated'); return redirect('admin/category'); } public function index_1() { $allcat=DB::select("select * from sub_category"); return view('admin.s_category', compact('allcat')); } public function s_manage_category_process(Request $request) { // Retrieve the ID from the request $id = $request->input('id'); // Check if an ID is provided if ($id) { // If ID is provided, update the existing category $category = Sub_User::findOrFail($id); $msg = "Category updated"; } else { // If no ID is provided, create a new category $category = new Sub_User(); $msg = "Category inserted"; } // Set category properties $category->Category_Id = $request->input('Category_Id'); $category->Sub_Category_Name = $request->input('Sub_Category_Name'); // Check if a new image is uploaded if ($request->hasFile('Sub_Category_Image')) { // Store the new image $image = $request->file('Sub_Category_Image'); $ext = $image->extension(); $image_name = time() . '.' . $ext; $image->storeAs('public/media/category', $image_name); $category->Sub_Category_Image = $image_name; } // Save the category $category->save(); // Redirect back to the sub_category_all route with a success message return redirect('admin/s_category')->with('success', $msg); } public function s_manage_category(Request $request, $id = '') { // Retrieve categories from the database $categories = DB::table('categories')->get(); $selected_category_id = ''; // Initialize selected category id variable if ($id > 0) { $category = Sub_User::findOrFail($id); $result['Category_Id'] = $category->Category_Id; $result['Sub_Category_Name'] = $category->Sub_Category_Name; $result['Sub_Category_Image'] = $category->Sub_Category_Image; $result['Is_Active'] = $category->Is_Active; $result['is_home_selected'] = $category->Is_Active == 1 ? "checked" : ""; $result['id'] = $category->id; $result['categories'] = Sub_User::where('status', 1)->where('id', '!=', $id)->get(); $selected_category_id = $result['Category_Id']; } else { $result['Category_Id'] = ''; $result['Sub_Category_Name'] = ''; $result['Sub_Category_Image'] = ''; $result['Is_Active'] = ''; $result['is_home_selected'] = ""; $result['id'] = 0; $result['categories'] = Sub_User::where('status', 1)->get(); } // Pass the selected category id along with other data to the view return view('admin/s_manage_category', ['categories' => $categories, 'id' => $id, 'selected_category_id' => $selected_category_id, 'Sub_Category_Name' => old('Sub_Category_Name', $result['Sub_Category_Name'] ?? '')]); } public function delete_1(Request $request, $id) { $category = Sub_User::findOrFail($id); Storage::delete('public/media/category/' . $category->CategoryImage); $category->delete(); $request->session()->flash('message', 'Category deleted'); return redirect('admin/s_category'); } public function status_1(Request $request, $status, $id) { $category = Sub_User::findOrFail($id); $category->status = $status; $category->save(); $request->session()->flash('message', 'Category status updated'); return redirect('admin/s_category'); } public function showAllImages() { $images = Product::pluck('image')->toArray(); return view('admin.s_category', ['images' => $images]); } }