File: D:/HostingSpaces/SBogers10/stafa.komma.pro/app/Komma/Events/EventService.php
<?php
namespace App\Komma\Events;
use App\Komma\Base\Service;
use App\Komma\Events\Models\Event;
use Carbon\Carbon;
final class EventService extends Service
{
private $today;
public function __construct()
{
$this->today = now()->endOfDay();
$this->today = $this->today->format('Y-m-d H:i:s');
parent::__construct();
}
/**
* Base query for get event from DB
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
private function baseEventQuery()
{
return $this->site
->events()
->with('translation', 'images')
->whereHas('translation')
->where('active', 1)
->where('date', '>=', $this->today)
->orderBy('date','asc')
->orderBy('created_at', 'desc');
}
/**
* Get all events
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getEvents()
{
return $this->baseEventQuery()->get();
}
/**
* Get $amount of latest events
*
* @param int $amount
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
*/
public function getLatestEvent($amount = 3)
{
return $this->baseEventQuery()
->take($amount)
->get();
}
public function getLastEvent()
{
return $this->baseEventQuery()
->first();
}
/**
* Get events paginated per $amount on a page
*
* @param int $amountPerPage
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
public function getEventsPaginated($amountPerPage = 6)
{
return $this->baseEventQuery()->paginate($amountPerPage);
}
/**
* Get other events based on the current $event
*
* @param Event $event
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
*/
public function getNextEvents(Event $event)
{
return $this->baseEventQuery()
->where('events.id', '!=', $event->id)
->where('date', '<=', $event->date->format(Carbon::DEFAULT_TO_STRING_FORMAT))
->take(2)
->get();
}
}