File: D:/HostingSpaces/SBogers10/reiskick.komma.nl/app/Countries/CountryService.php
<?php
namespace App\Countries;
use App\Articles\Models\Article;
use App\Journeys\Models\Journey;
use App\MustDos\Models\MustDo;
use App\ToDos\Models\ToDo;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
class CountryService
{
/**
* Get all the models
*
* @param Country $country
* @return Collection
*/
public function getRelatedModels(Country $country) : Collection
{
$country->load(
'mustDos', 'mustDos.translation', 'mustDos.headingImage',
'toDos', 'toDos.translation', 'toDos.headingImage',
'articles', 'articles.translation', 'articles.headingImage',
'overnights','overnights.translation','overnights.headingImage',
'journeys','journeys.translation','journeys.headingImage',
);
$sliderItems = collect();
$sliderItems = $sliderItems->merge($country->journeys);
$sliderItems = $sliderItems->merge($country->mustDos->where('show_in_slider', 1));
$sliderItems = $sliderItems->merge($country->toDos->where('show_in_slider', 1));
$sliderItems = $sliderItems->merge($country->articles->where('show_in_slider', 1));
$sliderItems = $sliderItems->merge($country->overnights->where('show_in_slider', 1));
return $sliderItems;
}
/**
* @param int $countryId
* @param Model|null $excludedModel
* @return Collection
*/
public function getSuggestions(int $countryId, Model $excludedModel = null): Collection
{
$country = Country::find($countryId);
$country->load('mustDos','toDos','articles','journeys');
$collection = collect();
if($country->journeys->isNotEmpty()) {
if(isset($excludedModel) && is_a($excludedModel, Journey::class)) {
if($country->journeys->where('id','!=', $excludedModel->id)->count() > 1)
$collection->push($country->journeys->where('id','!=', $excludedModel->id)->random());
}
else $collection->push($country->journeys->random());
}
if($country->mustDos->isNotEmpty()) {
if(isset($excludedModel) && is_a($excludedModel, MustDo::class)) {
if($country->mustDos->where('id','!=', $excludedModel->id)->count() > 1)
$collection->push($country->mustDos->where('id','!=', $excludedModel->id)->random());
}
else $collection->push($country->mustDos->random());
}
if($country->toDos->isNotEmpty()) {
if (isset($excludedModel) && is_a($excludedModel, ToDo::class)) {
if($country->toDos->where('id','!=', $excludedModel->id)->count() > 1)
$collection->push($country->toDos->where('id', '!=', $excludedModel->id)->random());
}
else $collection->push($country->toDos->random());
}
if($country->articles->isNotEmpty()) {
if (isset($excludedModel) && is_a($excludedModel, Article::class)) {
if($country->articles->where('id','!=', $excludedModel->id)->count() > 1)
$collection->push($country->articles->where('id', '!=', $excludedModel->id)->random());
}
else $collection->push($country->articles->random());
}
// To bad we can't add them by loading the collection.
foreach ($collection as $item) $item->load('translation','headingImage');
return $collection;
}
}