HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/app/Komma/Products/ProductService.php
<?php

namespace App\Komma\Products;

use App\Komma\Base\Service;
use App\Komma\Posts\Models\Post;
use App\Komma\Products\Models\Product;
use Illuminate\Support\Facades\DB;

class ProductService extends Service
{
    /**
     * Base query for get post from DB
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    private function baseQuery(array $excludedProducts = ['bumperball_rent'])
    {
        return Product::where('active', 1)
            ->with('translation', 'overviewImage')
            ->whereHas('translation', function ($q) {
                $q->whereNotNull('name')
                    ->where('name', '!=', '');
            })
            ->whereNotIn('code_name', $excludedProducts)
            ->orderBy('sort_order');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function getProducts()
    {
        return $this->baseQuery([])
            ->where('hide_on_site', 0)
            ->get();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function getProductsForType(int $productType = 0)
    {
        return $this->baseQuery([])
            ->where('hide_on_site', 0)
            ->where('product_type', $productType)
            ->orderBy('sort_order')
            ->get();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function getAmountOfProducts(int $amount = 2)
    {
        return $this->baseQuery()
            ->where('hide_on_site', 0)
            ->take($amount)
            ->get();
    }

    /**
     * Get other products from the same type
     *
     * @param Product $product
     * @param int $productType
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
     */
    public function getOtherProducts(Product $product, int $productType = 1)
    {
        $otherProducts = $this->baseQuery()
            ->where('hide_on_site', 0)
            ->where('sort_order', '>', $product->sort_order)
            ->where('product_type', $productType)
            ->take(2)
            ->orderBy('sort_order')
            ->get();

        if ($otherProducts->count() == 2) {
            return $otherProducts;
        }

        $firstProducts = $this->baseQuery()
            ->where('hide_on_site', 0)
            ->where('sort_order', '<', $product->sort_order)
            ->where('product_type', $productType)
            ->take(2)
            ->orderBy('sort_order')
            ->get();

        if ($otherProducts->count() == 0) {
            return $firstProducts;
        }

        return $otherProducts->push($firstProducts->first());
    }

    /**
     * @param array $productIds
     * @param int $amount
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
     */
    public function getActivityProductsWhereIn(array $productIds, int $amount = 3)
    {
        $rawOrder = DB::raw(sprintf('FIELD(id, %s)', implode(',', $productIds)));

        // Because of the orderByRaw we can't use the base query...

        return Product::where('active', 1)
            ->where('hide_on_site', 0)
            ->whereIn('id', $productIds)
            ->where('product_type', 0)
            ->with('translation', 'images')
            ->whereHas('translation', function ($q) {
                $q->whereNotNull('name')
                    ->where('name', '!=', '');
            })
            ->orderByRaw($rawOrder)
            ->take($amount)
            ->get();
    }
}