Header Ads

Header ADS

laravel cache implement for product details

use Illuminate\Support\Facades\Cache;


public function product($slug)
{
    $this->code_image();

    // Cache the product details for 1 hour
    $productt = Cache::remember("product_{$slug}", 60 * 60, function () use ($slug) {
        return Product::where('slug', '=', $slug)->firstOrFail();
    });

    // Increment the product views
    $productt->increment('views');

    // Currency handling
    $curr = Cache::remember('currency', 60 * 60, function () {
        return Session::has('currency')
            ? Currency::find(Session::get('currency'))
            : Currency::where('is_default', '=', 1)->first();
    });

    // Log product click
    $product_click = new ProductClick;
    $product_click->product_id = $productt->id;
    $product_click->date = Carbon::now()->format('Y-m-d');
    $product_click->save();

    // Get vendors' products
    $vendors = Cache::remember("vendors_{$productt->user_id}", 60 * 60, function () use ($productt) {
        return Product::where('status', '=', 1)
            ->where('user_id', '=', $productt->user_id ? $productt->user_id : 0)
            ->take(8)
            ->get();
    });

    return view('front.product', compact('productt', 'curr', 'vendors'));
}

No comments

Theme images by fpm. Powered by Blogger.