관리-도구
편집 파일: BaseController.php
<?php namespace App\Http\Controllers; use App\Models\About; use App\Models\Address; use App\Models\InstagramPost; use App\Models\Products; use App\Models\Review; use App\Models\Service; use App\Models\Story; use App\Models\Blog; use App\Models\Testimonial; use App\Models\Policies; use App\Models\Client; use App\Models\Category; use App\Models\Slider; use Dotenv\Exception\ValidationException; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; use App\Models\Faq; use App\Models\HotOffer; use Illuminate\Support\Facades\Auth; class BaseController extends Controller { public function index() { $posts = InstagramPost::latest()->take(12)->get(); // Limit to 12 posts $categories = Category::all(); $hotoffer = HotOffer::All(); $client = Client::All(); $address = Address::first(); $hotofferr = HotOffer::with('products.images') ->orderBy('created_at', 'desc') // latest products first ->take(3) ->get(); $sliders = Slider::all(); // Fetch 8 products ordered by display_order $products = Products::with('images') ->orderBy('created_at', 'desc') // latest products first ->take(8) ->get(); $about = About::where('theme', 1)->first(); $address = Address::first(); return view('frontend.index', compact('sliders', 'address', 'products', 'about', 'posts', 'categories', 'hotoffer', 'client', 'hotofferr', 'address')); } public function about() { // Fetch FAQs from the database $faqs = Faq::all(); // Other about-us content $aboutBlocks = About::whereIn('theme', [1, 2, 3, 4])->get()->keyBy('theme'); $story = Story::all(); $testimonial = Testimonial::all(); $address = Address::first(); return view('frontend.about', compact('aboutBlocks', 'faqs', 'story', 'testimonial', 'address')); } public function service() { // Fetch FAQs from the database $faqs = Faq::all(); // Other about-us content $services = Service::all(); $address = Address::first(); return view('frontend.service', compact('services', 'faqs', 'address')); } public function blog() { $address = Address::first(); $blog = Blog::all(); return view('frontend.blog', compact('blog', 'address')); } public function blogdetails($slug) { $address = Address::first(); // Main blog by slug $blogs = Blog::where('slug', $slug)->firstOrFail(); // Related blogs (exclude current blog) $relatedBlogs = Blog::where('id', '!=', $blogs->id) ->latest() ->limit(6) ->get(); return view( 'frontend.blogdetails', compact('blogs', 'relatedBlogs', 'address') ); } public function products(Request $request) { $category = $request->input('category'); $hotoffer = $request->input('hotoffer'); // Eager-load images so images are always available $query = Products::with('images'); // Filter by Category if ($category) { $query->where('cat_id', $category); } // Filter by HotOffer if ($hotoffer) { $query->where('hotoff_id', $hotoffer); } $shops = $query->orderBy('created_at', 'DESC')->get(); $categories = Category::all(); $hotoffers = HotOffer::all(); $address = Address::first(); return view('frontend.shop', compact('shops', 'categories', 'hotoffers', 'address')); } public function productdetail($id) { $productdetail = Products::with('images')->findOrFail($id); $address = Address::first(); return view('frontend.productdetail', compact('productdetail', 'address')); } public function filter($subcat_id) { $products = Products::with('images')->where('subcat_id', $subcat_id)->get(); $categories = Category::with('subcategories')->get(); return view('frontend.shop', compact('products', 'categories')); } public function filterByCategory($cat_id) { $products = Products::with('images')->where('cat_id', $cat_id)->get(); $categories = Category::with('subcategories')->get(); return view('frontend.shop', compact('products', 'categories')); } public function checkout(Request $request) { $product = null; $address = Address::first(); if ($request->has('product_id')) { $product = Products::find($request->product_id); } return view('frontend.checkout', compact('product', 'address')); } public function contact() { $address = Address::first(); return view('frontend.contactus', compact('address')); } public function login() { return view('frontend.login'); } public function shipping() { return view('frontend.shipping'); } public function shop() { $categories = Category::with('subcategories')->get(); // Fetch all products ordered by display_order $products = Products::with(['images', 'category', 'subcategory']) ->orderBy('display_order', 'asc') // ← order by display_order ->get(); return view('frontend.shop', compact('products', 'categories')); } public function policy($slug) { // Match by slug or by title $policy = Policies::where('slug', $slug)->firstOrFail(); return view('frontend.shipping', compact('policy')); } public function singleProduct($id, $slug) { $product = Products::with([ 'images', 'reviews' => function ($query) { $query->where('is_blocked', '!=', 1); } ])->findOrFail($id); // Optional: check if slug matches the product's slug and redirect if not if ($slug !== $product->slug) { return redirect()->route('singleProduct', ['id' => $product->id, 'slug' => $product->slug]); } // Fetch related products (same category, excluding current product) $relatedProducts = Products::with('images') ->where('cat_id', $product->cat_id) ->where('id', '!=', $product->id) ->take(8) ->get(); return view('frontend.single-product', compact('product', 'relatedProducts')); } public function store(Request $request) { // If not logged in → redirect to login page if (!Auth::check()) { return redirect()->route('login')->with('error', 'Please login to write a review.'); } $validated = $request->validate([ 'product_id' => 'required|integer', 'name' => 'required|string|max:255', 'rating' => 'required|integer|min:1|max:5', 'review' => 'required|string', ]); $validated['is_blocked'] = 0; Review::create($validated); return redirect()->back()->with('success', 'Review submitted successfully!'); } }