File: D:/HostingSpaces/SBogers10/rentman2019.komma.pro/app/Komma/Crews/CrewService.php
<?php
namespace App\Komma\Crews;
use App\Komma\Base\Service;
use Illuminate\Support\Collection;
class CrewService extends Service
{
public function getCrews($pagination = false, $itemsPerPage = 9)
{
$crews = $this->site
->crews()
->with('translation', 'images', 'fallback_translation')
->orderBy('lft')
// We need to use a join to select the active crews because that is defined on the translations
->join('crew_translations', 'crews.id', '=', 'crew_translations.crew_id')
->select('crews.*', 'crew_translations.active', 'crew_translations.populate_from_english', 'crew_translations.language_id')
->where(function ($query) {
$query->where('active', '=', 1)
->orWhere('populate_from_english', '=', 1);
})
->where('language_id', \App::getLanguage()->id);
if ($pagination) {
$crews = $crews->paginate($itemsPerPage);
// Skip the population if language is already english
if (app()->getLocale() != 'en') {
$this->populatedFallbackTranslationOnCollection($crews->getCollection());
}
} else {
$crews = $crews->get();
// Skip the population if language is already english
if (app()->getLocale() != 'en') {
$this->populatedFallbackTranslationOnCollection($crews);
}
}
return $crews;
}
private function populatedFallbackTranslationOnCollection(Collection $crews)
{
foreach ($crews as $crew) {
// Skip if we don't need to populated
if (! $crew->translation->populate_from_english) {
continue;
}
$crew->translation = $crew->fallback_translation;
$crew->used_fallback_translation = true;
}
}
}