File: D:/HostingSpaces/SBogers10/inzigd.komma.pro/app/Komma/Workshops/WorkshopService.php
<?php
namespace App\Komma\Workshops;
use App\Komma\Base\Service;
use App\Komma\Categories\Models\Category;
use App\Komma\Pages\Models\Page;
use App\Komma\Workshops\Models\Workshop;
use Carbon\Carbon;
final class WorkshopService extends Service
{
/**
* Base query for get workshop from DB
*
* @return Workshop|\Illuminate\Database\Eloquent\Builder
*/
private function baseWorkshopQuery()
{
return Workshop::with('translation', 'images')
->where('active', 1)
->orderBy('lft', 'asc');
}
/**
* Get all workshops
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getWorkshops()
{
return $this->baseWorkshopQuery()->get();
}
/**
* Get $amount of latest workshops
*
* @param int $amount
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
*/
public function getLatestWorkshop($amount = 3)
{
return $this->baseWorkshopQuery()
->take($amount)
->get();
}
/**
* Get workshops paginated per $amount on a page
* We get thses through the category
*
* @param int $amountPerPage
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
public function getWorkshopsByCategoryPaginated(Page $categoryPage, $amountPerPage = 40)
{
$category = null;
switch ($categoryPage->code_name) {
case 'teaching':
$category = Category::find(1);
break;
case 'enterprising':
$category = Category::find(2);
break;
default:
dd($categoryPage);
break;
}
$workshopIds = $category->workshops->pluck('id')->toArray();
return $this->baseWorkshopQuery()
->whereIn('id', $workshopIds)
->paginate($amountPerPage);
}
/**
* Get the next workshops after give $workshop
*
* @param Workshop $workshop
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
*/
public function getNextWorkshops(Workshop $workshop)
{
return $this->baseWorkshopQuery()
->where('id', '!=', $workshop->id)
->where('lft', '<=', $workshop->lft)
->take(2)
->get();
}
}