👁 查看:ProductController.php.1
路径:/home/boguraj1/.trash/ProductController.php.1
大小:1.65 KB · 修改:2026-07-23 16:52:26 · 权限:0644 · 可写
<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Models\Product;
use App\Models\Category;
use App\Services\MetaConversionsService;

class ProductController extends Controller
{
    public function index(\Illuminate\Http\Request $request)
    {
        $query = Product::active()->with('category');

        if ($request->filled('search')) {
            $query->where('name', 'like', '%' . $request->search . '%');
        }
        if ($request->filled('category')) {
            $query->whereHas('category', fn($q) => $q->where('slug', $request->category));
        }

        $products   = $query->latest()->orderBy('sort_order')->paginate(20)->withQueryString();
        $categories = Category::where('status', true)->withCount('products')->get();

        return view('frontend.shop', compact('products', 'categories'));
    }

    public function show(string $slug)
    {
        $product = Product::active()
            ->with(['category', 'images', 'variations', 'tags'])
            ->where('slug', $slug)
            ->firstOrFail();

        $related = Product::active()
            ->where('category_id', $product->category_id)
            ->where('id', '!=', $product->id)
            ->take(5)
            ->get();

        // Server-side ViewContent event + get event_id for browser dedup
        $capi = app(MetaConversionsService::class);
        $pixelEventId = $capi->sendViewContent(
            $product->id,
            $product->name,
            (float) ($product->sale_price ?? $product->regular_price ?? 0)
        );

        return view('frontend.product', compact('product', 'related', 'pixelEventId'));
    }
}