route($request->user()->role); } public function home(){ $featured=Product::where('status','active')->where('is_featured',1)->orderBy('price','DESC')->limit(2)->get(); $posts=Post::where('status','active')->orderBy('id','DESC')->limit(3)->get(); $banners=Banner::where('status','active')->limit(3)->orderBy('id','DESC')->get(); // return $banner; $products=Product::where('status','active')->orderBy('id','DESC')->limit(8)->get(); $category=Category::where('status','active')->where('is_parent',1)->orderBy('title','ASC')->get(); // return $category; return view('frontend.index') ->with('featured',$featured) ->with('posts',$posts) ->with('banners',$banners) ->with('product_lists',$products) ->with('category_lists',$category); } public function aboutUs(){ return view('frontend.pages.about-us'); } public function contact(){ return view('frontend.pages.contact'); } //Additional Pages public function faq() { return view('frontend.pages.faq'); } public function terms() { return view('frontend.pages.terms'); } public function privacy() { return view('frontend.pages.privacy'); } public function returns() { return view('frontend.pages.returns'); } public function moneyBack() { return view('frontend.pages.money-back'); } public function shipping() { return view('frontend.pages.shipping'); } // public function productDetail($slug){ // $product_detail= Product::getProductBySlug($slug); // // dd($product_detail); // return view('frontend.pages.product_detail')->with('product_detail',$product_detail); // } public function productDetail($slug) { // Get the current product details $product_detail = Product::getProductBySlug($slug); // Check if the child category is available in the product details $category_id = $product_detail->child_cat_id; // Ensure it's set correctly if (!$category_id) { // If no category is found, we might want to handle the case or fallback to some default return redirect()->back()->with('error', 'Product category not found.'); } // Get related products by matching category_id (exclude the current product) $related_products = Product::where('status', 'active') // only active products ->where('child_cat_id', $category_id) // filter by category ->where('id', '!=', $product_detail->id) // exclude the current product ->limit(8) // limit to 8 related products ->get(); // Check if there are related products if ($related_products->isEmpty()) { // Handle if no related products are found, maybe display a message or fallback $related_products = []; } // Pass the product details and related products to the view return view('frontend.pages.product_detail', [ 'product_detail' => $product_detail, 'related_products' => $related_products, ]); } public function productGrids(){ $products=Product::query(); if(!empty($_GET['category'])){ $slug=explode(',',$_GET['category']); // dd($slug); $cat_ids=Category::select('id')->whereIn('slug',$slug)->pluck('id')->toArray(); // dd($cat_ids); $products->whereIn('cat_id',$cat_ids); // return $products; } if(!empty($_GET['brand'])){ $slugs=explode(',',$_GET['brand']); $brand_ids=Brand::select('id')->whereIn('slug',$slugs)->pluck('id')->toArray(); return $brand_ids; $products->whereIn('brand_id',$brand_ids); } if(!empty($_GET['sortBy'])){ if($_GET['sortBy']=='title'){ $products=$products->where('status','active')->orderBy('title','ASC'); } if($_GET['sortBy']=='price'){ $products=$products->orderBy('price','ASC'); } } if(!empty($_GET['price'])){ $price=explode('-',$_GET['price']); // return $price; // if(isset($price[0]) && is_numeric($price[0])) $price[0]=floor(Helper::base_amount($price[0])); // if(isset($price[1]) && is_numeric($price[1])) $price[1]=ceil(Helper::base_amount($price[1])); $products->whereBetween('price',$price); } $recent_products=Product::where('status','active')->orderBy('id','DESC')->limit(3)->get(); // Sort by number if(!empty($_GET['show'])){ $products=$products->where('status','active')->paginate($_GET['show']); } else{ $products=$products->where('status','active')->paginate(9); } // Sort by name , price, category return view('frontend.pages.product-grids')->with('products',$products)->with('recent_products',$recent_products); } public function productLists(){ $products=Product::query(); if(!empty($_GET['category'])){ $slug=explode(',',$_GET['category']); // dd($slug); $cat_ids=Category::select('id')->whereIn('slug',$slug)->pluck('id')->toArray(); // dd($cat_ids); $products->whereIn('cat_id',$cat_ids)->paginate; // return $products; } if(!empty($_GET['brand'])){ $slugs=explode(',',$_GET['brand']); $brand_ids=Brand::select('id')->whereIn('slug',$slugs)->pluck('id')->toArray(); return $brand_ids; $products->whereIn('brand_id',$brand_ids); } if(!empty($_GET['sortBy'])){ if($_GET['sortBy']=='title'){ $products=$products->where('status','active')->orderBy('title','ASC'); } if($_GET['sortBy']=='price'){ $products=$products->orderBy('price','ASC'); } } if(!empty($_GET['price'])){ $price=explode('-',$_GET['price']); // return $price; // if(isset($price[0]) && is_numeric($price[0])) $price[0]=floor(Helper::base_amount($price[0])); // if(isset($price[1]) && is_numeric($price[1])) $price[1]=ceil(Helper::base_amount($price[1])); $products->whereBetween('price',$price); } $recent_products=Product::where('status','active')->orderBy('id','DESC')->limit(3)->get(); // Sort by number if(!empty($_GET['show'])){ $products=$products->where('status','active')->paginate($_GET['show']); } else{ $products=$products->where('status','active')->paginate(6); } // Sort by name , price, category return view('frontend.pages.product-lists')->with('products',$products)->with('recent_products',$recent_products); } public function productFilter(Request $request){ $data= $request->all(); // return $data; $showURL=""; if(!empty($data['show'])){ $showURL .='&show='.$data['show']; } $sortByURL=''; if(!empty($data['sortBy'])){ $sortByURL .='&sortBy='.$data['sortBy']; } $catURL=""; if(!empty($data['category'])){ foreach($data['category'] as $category){ if(empty($catURL)){ $catURL .='&category='.$category; } else{ $catURL .=','.$category; } } } $brandURL=""; if(!empty($data['brand'])){ foreach($data['brand'] as $brand){ if(empty($brandURL)){ $brandURL .='&brand='.$brand; } else{ $brandURL .=','.$brand; } } } // return $brandURL; $priceRangeURL=""; if(!empty($data['price_range'])){ $priceRangeURL .='&price='.$data['price_range']; } if(request()->is('e-shop.loc/product-grids')){ return redirect()->route('product-grids',$catURL.$brandURL.$priceRangeURL.$showURL.$sortByURL); } else{ return redirect()->route('product-lists',$catURL.$brandURL.$priceRangeURL.$showURL.$sortByURL); } } // public function productSearch(Request $request){ // $recent_products=Product::where('status','active')->orderBy('id','DESC')->limit(3)->get(); // $products=Product::orwhere('title','like','%'.$request->search.'%') // ->orwhere('slug','like','%'.$request->search.'%') // ->orwhere('description','like','%'.$request->search.'%') // ->orwhere('summary','like','%'.$request->search.'%') // ->orwhere('price','like','%'.$request->search.'%') // ->orderBy('id','DESC') // ->paginate('9'); // return view('frontend.pages.product-grids')->with('products',$products)->with('recent_products',$recent_products); // } public function productSearch(Request $request) { // Get q from query; fall back to 'search' for old forms $q = $request->filled('q') ? $request->input('q') : $request->input('search'); $q = is_string($q) ? trim($q) : ''; $view = $request->get('view', 'grid'); // 'grid' or 'list' $productsQuery = \App\Models\Product::query(); if ($q !== '') { $productsQuery->where(function ($qq) use ($q) { $qq->where('title', 'like', "%{$q}%") ->orWhere('slug', 'like', "%{$q}%") ->orWhere('description', 'like', "%{$q}%") ->orWhere('summary', 'like', "%{$q}%") ->orWhere('price', 'like', "%{$q}%"); }); } $products = $productsQuery ->orderBy('id', 'DESC') ->paginate(9) ->appends($request->query()); // preserve q/view/etc. on links $recent_products = \App\Models\Product::where('status', 'active') ->orderBy('id', 'DESC') ->limit(3) ->get(); // If you only have one Blade (e.g., product-grids), you can force that: $viewName = ($view === 'list') ? 'frontend.pages.product-lists' : 'frontend.pages.product-grids'; // Optional: Guard against missing view if (!view()->exists($viewName)) { $viewName = 'frontend.pages.product-grids'; } return view($viewName, compact('products', 'recent_products', 'view', 'q')); } public function productBrand(Request $request){ $products=Brand::getProductByBrand($request->slug); $recent_products=Product::where('status','active')->orderBy('id','DESC')->limit(3)->get(); if(request()->is('e-shop.loc/product-grids')){ return view('frontend.pages.product-grids')->with('products',$products->products)->with('recent_products',$recent_products); } else{ return view('frontend.pages.product-lists')->with('products',$products->products)->with('recent_products',$recent_products); } } // public function productCat(Request $request){ // $products=Category::getProductByCat($request->slug); // // return $request->slug; // $recent_products=Product::where('status','active')->orderBy('id','DESC')->limit(3)->get(); // if(request()->is('e-shop.loc/product-grids')){ // return view('frontend.pages.product-grids')->with('products',$products->products)->with('recent_products',$recent_products); // } // else{ // return view('frontend.pages.product-lists')->with('products',$products->products)->with('recent_products',$recent_products); // } // } public function productCat($slug) { $category = Category::where('slug', $slug)->firstOrFail(); $products = Product::where('cat_id', $category->id) ->paginate(12); $view = request()->get('view', 'grid'); // 👇 Fetch 6 recent products $recent_products = Product::latest()->take(6)->get(); return view('frontend.pages.product-lists', compact('category', 'products', 'view', 'recent_products')); } public function productSubCat(Request $request){ $products=Category::getProductBySubCat($request->sub_slug); // return $products; $recent_products=Product::where('status','active')->orderBy('id','DESC')->limit(3)->get(); if(request()->is('e-shop.loc/product-grids')){ return view('frontend.pages.product-grids')->with('products',$products->sub_products)->with('recent_products',$recent_products); } else{ return view('frontend.pages.product-lists')->with('products',$products->sub_products)->with('recent_products',$recent_products); } } public function blog(){ $post=Post::query(); if(!empty($_GET['category'])){ $slug=explode(',',$_GET['category']); // dd($slug); $cat_ids=PostCategory::select('id')->whereIn('slug',$slug)->pluck('id')->toArray(); return $cat_ids; $post->whereIn('post_cat_id',$cat_ids); // return $post; } if(!empty($_GET['tag'])){ $slug=explode(',',$_GET['tag']); // dd($slug); $tag_ids=PostTag::select('id')->whereIn('slug',$slug)->pluck('id')->toArray(); // return $tag_ids; $post->where('post_tag_id',$tag_ids); // return $post; } if(!empty($_GET['show'])){ $post=$post->where('status','active')->orderBy('id','DESC')->paginate($_GET['show']); } else{ $post=$post->where('status','active')->orderBy('id','DESC')->paginate(9); } // $post=Post::where('status','active')->paginate(8); $rcnt_post=Post::where('status','active')->orderBy('id','DESC')->limit(3)->get(); return view('frontend.pages.blog')->with('posts',$post)->with('recent_posts',$rcnt_post); } public function blogDetail($slug){ $post=Post::getPostBySlug($slug); $rcnt_post=Post::where('status','active')->orderBy('id','DESC')->limit(3)->get(); // return $post; return view('frontend.pages.blog-detail')->with('post',$post)->with('recent_posts',$rcnt_post); } public function blogSearch(Request $request){ // return $request->all(); $rcnt_post=Post::where('status','active')->orderBy('id','DESC')->limit(3)->get(); $posts=Post::orwhere('title','like','%'.$request->search.'%') ->orwhere('quote','like','%'.$request->search.'%') ->orwhere('summary','like','%'.$request->search.'%') ->orwhere('description','like','%'.$request->search.'%') ->orwhere('slug','like','%'.$request->search.'%') ->orderBy('id','DESC') ->paginate(8); return view('frontend.pages.blog')->with('posts',$posts)->with('recent_posts',$rcnt_post); } public function blogFilter(Request $request){ $data=$request->all(); // return $data; $catURL=""; if(!empty($data['category'])){ foreach($data['category'] as $category){ if(empty($catURL)){ $catURL .='&category='.$category; } else{ $catURL .=','.$category; } } } $tagURL=""; if(!empty($data['tag'])){ foreach($data['tag'] as $tag){ if(empty($tagURL)){ $tagURL .='&tag='.$tag; } else{ $tagURL .=','.$tag; } } } // return $tagURL; // return $catURL; return redirect()->route('blog',$catURL.$tagURL); } public function blogByCategory(Request $request){ $post=PostCategory::getBlogByCategory($request->slug); $rcnt_post=Post::where('status','active')->orderBy('id','DESC')->limit(3)->get(); return view('frontend.pages.blog')->with('posts',$post->post)->with('recent_posts',$rcnt_post); } public function blogByTag(Request $request){ // dd($request->slug); $post=Post::getBlogByTag($request->slug); // return $post; $rcnt_post=Post::where('status','active')->orderBy('id','DESC')->limit(3)->get(); return view('frontend.pages.blog')->with('posts',$post)->with('recent_posts',$rcnt_post); } // Login public function login(){ return view('frontend.pages.login'); } public function loginSubmit(Request $request){ $data= $request->all(); if(Auth::attempt(['email' => $data['email'], 'password' => $data['password'],'status'=>'active'])){ Session::put('user',$data['email']); request()->session()->flash('success','Successfully login'); return redirect()->route('home'); } else{ request()->session()->flash('error','Invalid email and password pleas try again!'); return redirect()->back(); } } public function logout(){ Session::forget('user'); Auth::logout(); request()->session()->flash('success','Logout successfully'); return back(); } public function register(){ return view('frontend.pages.register'); } public function registerSubmit(Request $request){ // return $request->all(); $this->validate($request,[ 'name'=>'string|required|min:2', 'email'=>'string|required|unique:users,email', 'password'=>'required|min:6|confirmed', ]); $data=$request->all(); // dd($data); $check=$this->create($data); Session::put('user',$data['email']); if($check){ request()->session()->flash('success','Successfully registered'); return redirect()->route('home'); } else{ request()->session()->flash('error','Please try again!'); return back(); } } public function create(array $data){ return User::create([ 'name'=>$data['name'], 'email'=>$data['email'], 'password'=>Hash::make($data['password']), 'status'=>'active' ]); } // Reset password public function showResetForm(){ return view('auth.passwords.old-reset'); } public function subscribe(Request $request){ if(! Newsletter::isSubscribed($request->email)){ Newsletter::subscribePending($request->email); if(Newsletter::lastActionSucceeded()){ request()->session()->flash('success','Subscribed! Please check your email'); return redirect()->route('home'); } else{ Newsletter::getLastError(); return back()->with('error','Something went wrong! please try again'); } } else{ request()->session()->flash('error','Already Subscribed'); return back(); } } }