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/rentman2019.komma.pro/app/Komma/Inventories/InventoryService.php
<?php

namespace App\Komma\Inventories;

use App\Komma\Base\Service;
use Illuminate\Support\Collection;

class InventoryService extends Service
{
    public function getInventories($pagination = false, $itemsPerPage = 9)
    {
        $inventories = $this->site
            ->inventories()
            ->with('translation', 'images', 'fallback_translation')
            ->orderBy('lft')

            // We need to use a join to select the active inventories because that is defined on the translations
            ->join('inventory_translations', 'inventories.id', '=', 'inventory_translations.inventory_id')
            ->select('inventories.*', 'inventory_translations.active', 'inventory_translations.populate_from_english', 'inventory_translations.language_id')
            ->where(function ($query) {
                $query->where('active', '=', 1)
                    ->orWhere('populate_from_english', '=', 1);
            })
            ->where('language_id', \App::getLanguage()->id);

        if ($pagination) {
            $inventories = $inventories->paginate($itemsPerPage);

            // Skip the population if language is already english
            if (app()->getLocale() != 'en') {
                $this->populatedFallbackTranslationOnCollection($inventories->getCollection());
            }
        } else {
            $inventories = $inventories->get();

            // Skip the population if language is already english
            if (app()->getLocale() != 'en') {
                $this->populatedFallbackTranslationOnCollection($inventories);
            }
        }

        return $inventories;
    }

    private function populatedFallbackTranslationOnCollection(Collection $inventories)
    {
        foreach ($inventories as $inventory) {
            // Skip if we don't need to populated
            if (! $inventory->translation->populate_from_english) {
                continue;
            }
            $inventory->translation = $inventory->fallback_translation;
            $inventory->used_fallback_translation = true;
        }
    }
}