PHP 8.2.30
Preview: ProductRepository.php Size: 2.52 KB
/home/byroehnu/easetack.com/app/Repositories/Frontend/ProductRepository.php

<?php

namespace App\Repositories\Frontend;

use App\Models\Product;
use Illuminate\Support\Facades\Cache;

class ProductRepository
{
    /**
     * Object model will be used to modify blogs table
     */
    protected Product $model;

    /**
     * Constructor for blog repository
     */
    public function __construct(Product $product)
    {
        $this->model = $product;
    }

    /**
     * Get search result with paginate
     *
     * @param  array  $sort
     */
    public function paginateSearchResult($search, array $filter = [], $sort = 'latest')
    {
        $query = $this->model->with(['content', 'contents'])->newQuery();

        // Apply search filters if necessary
        if (! empty($search)) {
            $query->whereHas('contents', function ($q) use ($search) {
                $q->where('title', 'like', "%{$search}%")
                    ->orWhere('title', 'like', "%{$search}%");
            })->orWhereHas('category.contents', function ($q) use ($search) {
                $q->where('title', 'like', "%{$search}%");
            })->orWhereHas('tags', function ($q) use ($search) {
                $q->where('name', 'like', "%{$search}%");
            });
        }

        if (! empty($filter['category'])) {
            $query->whereHas('category.content', function ($q) use ($filter) {
                $q->where('title', $filter['category']);
            });
        }

        if (! empty($filter['tag'])) {
            $query->withAnyTags([$filter['tag']]);
        }

        if (isset($filter['min_price']) && isset($filter['max_price'])) {
            $query->whereBetween('price', [
                $filter['min_price'],
                $filter['max_price'],
            ]);
        }
        if (! empty($filter['sort'])) {
            switch ($filter['sort']) {
                case 'latest':
                    $query->latest();
                    break;
                case 'low':
                    $query->orderBy('price', 'asc');
                    break;
                case 'high':
                    $query->orderBy('price', 'desc');
                    break;
                default:
                    $query->latest();
                    break;
            }
        } else {
            $query->latest();
        }

        return $query->where('status', '1')->paginate(perPage: 9);
    }

    public function getMaxProductPrice()
    {
        return Cache::remember('max_product_price', 3600, function () {
            return Product::selectRaw('MAX(price) as max_price')->value('max_price');
        });
    }
}

Directory Contents

Dirs: 0 × Files: 9

Name Size Perms Modified Actions
2.62 KB lrw-rw-rw- 2025-04-19 04:22:15
Edit Download
8.26 KB lrw-rw-rw- 2025-05-15 11:24:11
Edit Download
1.45 KB lrw-rw-rw- 2025-04-19 04:22:15
Edit Download
6.38 KB lrw-rw-rw- 2025-05-08 12:05:13
Edit Download
2.52 KB lrw-rw-rw- 2025-04-26 11:30:31
Edit Download
1.77 KB lrw-rw-rw- 2025-04-15 10:33:57
Edit Download
829 B lrw-rw-rw- 2025-04-19 04:22:16
Edit Download
5.46 KB lrw-rw-rw- 2025-04-24 11:21:09
Edit Download
3.50 KB lrw-rw-rw- 2025-04-13 07:02:31
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).