HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/inzigd.komma.pro/app/Komma/Courses/CourseService.php
<?php


namespace App\Komma\Courses;


use App\Komma\Base\Service;
use App\Komma\Categories\Models\Category;
use App\Komma\Courses\Models\Course;
use App\Komma\Pages\Models\Page;
use Carbon\Carbon;

final class CourseService extends Service
{

    /**
     * Base query for get course from DB
     *
     * @return Course|\Illuminate\Database\Eloquent\Builder
     */
    private function baseCourseQuery()
    {
        return Course::with('translation', 'images')
            ->where('active', 1)
            ->orderBy('lft', 'asc');
    }

    /**
     * Get all courses
     *
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function getCourses()
    {
        return $this->baseCourseQuery()->get();
    }

    /**
     * Get $amount of latest courses
     *
     * @param  int  $amount
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
     */
    public function getLatestCourse($amount = 3)
    {
        return $this->baseCourseQuery()
            ->take($amount)
            ->get();
    }

    /**
     * Get courses paginated per $amount on a page
     *
     * @param  int  $amountPerPage
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getCoursesByCategoryPaginated(Page $categoryPage, $amountPerPage = 40)
    {

        $category = null;
        switch ($categoryPage->code_name) {
            case 'teaching':
                $category = Category::find(Category::TEACHING);
                break;
            case 'enterprising':
                $category = Category::find(Category::ENTERPRICING);
                break;
            default:
                dd($categoryPage);
                break;
        }

        $courseIds = $category->courses->pluck('id')->toArray();

        return $this->baseCourseQuery()
            ->whereIn('id', $courseIds)
            ->paginate($amountPerPage);
    }

    /**
     * Get the next courses after give $course
     *
     * @param  Course  $course
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
     */
    public function getNextCourses(Course $course)
    {
        return $this->baseCourseQuery()
            ->where('id', '!=', $course->id)
            ->where('lft', '<=', $course->lft)
            ->take(2)
            ->get();
    }


}