File: D:/HostingSpaces/SBogers10/beat-the-barn.komma.nl/app/Services/ServiceService.php
<?php
namespace App\Services;
use App\Services\Models\Service;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
class ServiceService extends Service
{
private $today;
public function __construct()
{
$this->today = Carbon::now()->addHour();
$this->today = $this->today->format('Y-m-d H:i:s');
parent::__construct();
}
/**
* Base query for get services from DB
*/
public function baseServiceQuery()
{
return Service::with('translation', 'images')
->where('active',1)
->orderBy('lft')
->whereHas('translation');
}
public function getAllServices()
{
return $this->baseServiceQuery()->get();
}
/**
* Get the next services, if no more next, fill in missing ones starting from the first
*
* @param Service $service
* @param int $amount
* @return Collection|\Illuminate\Support\Collection
*/
public function getNextServices(Service $service, $amount = 4)
{
$nextServices = $this->baseServiceQuery()
->where('services.id', '!=', $service->id)
->where('services.lft', '>', $service->lft)
->take($amount)
->get();
if($nextServices->count() >= $amount) return $nextServices;
$missingServices = $this->baseServiceQuery()
->where('services.id', '!=', $service->id)
->whereNotIn('services.id', $nextServices->pluck('id')->toArray())
->take($amount - $nextServices->count() )
->get();
return $nextServices->merge($missingServices);
}
}