join('categories', 'categories.category_slug', '=', 'products.category_id')// joining the contacts table , where user_id and contact_user_id are same ->select('products.*', 'categories.category_name') ->get(); return view('admin.product' , $result); } else { return redirect('login')->withErrors(['error' => 'Access denied. Please log in to continue.']); } } public function add_product(Request $request , $id='') { if($id > 0) { $arr=Product::Where(['product_slug'=>$id])->get(); $result['category_id']=$arr['0']->category_id; $result['product_name']=$arr['0']->product_name; $result['product_dis']=$arr['0']->product_dis; $result['product_imgpath']=$arr['0']->product_imgpath; $result['id']=$arr['0']->id; $result['category']=Category::all(); } else { $result['category_id']=''; $result['product_name']=''; $result['product_dis']=''; $result['product_imgpath']=''; $result['id']=0; $result['category']=Category::all(); } return view('admin.add_product', $result); } public function add_product_process(Request $request) { $request->validate([ 'category_id'=>'required', 'product_name'=>'required', 'product_dis'=>'required', // 'product_imgpath'=>'required|mimes:jpeg,png,jpg,gif' 'product_imgpath' => $request->post('id') == 0 ? 'required|mimes:jpeg,png,jpg,gif' : '', ]); if($request->post('id') > 0) { $product=Product::find($request->post('id')); $msg="Product updated."; } else { $product = new Product ; $msg="Product inserted."; } $oldImage = $product->product_imgpath; $product->category_id=$request->post('category_id'); $slug = Str::slug($request->post('product_name'),'-'); $product->product_slug = $slug ; $product->product_name=$request->post('product_name'); $product->product_dis=$request->post('product_dis'); if ($image = $request->file('product_imgpath')) { $destinationPath = 'productImage/'; $profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension(); $image->move($destinationPath, $profileImage); $product->product_imgpath = $profileImage; if ($oldImage && $oldImage !== $product->product_imgpath) { $oldImagePath = public_path('productImage/' . $oldImage); if (file_exists($oldImagePath)) { unlink($oldImagePath); } } } else{ unset($product->product_imgpath); } $product->isActive=1; $product->save(); $request->session()->flash('message', $msg); return redirect('admin/product'); } public function delete($id) { $model=Product::find($id); if ($model != null) { $fileToDelete = public_path('productImage/' . $model->product_imgpath); if (File::exists($fileToDelete)) { File::delete($fileToDelete); } $model->delete(); return redirect()->back()->with(['error' => 'product Deleted...']); } } public function isactive($isactive , $id) { $model=Product::find($id); $model->isActive=$isactive; $model->save(); return redirect()->back()->with(['success' => 'product status updated...']); } }