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/Http/Wildcards/WorkshopsWildcard.php
<?php

namespace App\Http\Wildcards;

use App\Komma\Categories\Models\Category;
use App\Komma\Courses\Models\CourseTranslation;
use App\Komma\Workshops\Models\WorkshopTranslation;

class WorkshopsWildcard implements WildcardInterface
{

    /**
     * @param $request
     * @param $wildcard
     * @return mixed
     */
    public function handle($request, $wildcard)
    {

        // Get the category code name of the workshop
        $typeCodeName = $wildcard->type->pageTranslation->translatable->getParent()->code_name;
        switch ($typeCodeName) {
            case 'teaching':
                $categoryId = Category::TEACHING;
                break;
            case 'enterprising':
                $categoryId = Category::ENTERPRICING;
                break;
            default:
                Log::alert('WorkshopWildcard: Category not found ' . $typeCodeName);
                return $request;
        }


        // Get the workshops with that are the same as the found slug
        $workshopTranslations = WorkshopTranslation::where('slug', $wildcard->tail[0])
            ->with('translatable', 'translatable.category')
            ->has('translatable')
            ->has('translatable.category')
            ->get();

        // If there isn't a translation found continue request
        if($workshopTranslations->isEmpty()) return $request;

        // Loop through the workshops with the right slug
        // We do this because workshop can be in both categories so we need to make sure the categories matches the wildcard type
        foreach ($workshopTranslations as $workshopTranslation) {

            $workshopCategory = $workshopTranslation->translatable->category->first();

            // If the category of the workshop doesn't match continue
            if($workshopCategory->id != $categoryId) continue;

            // Category match so this the correct workshop

            //Set the request URI and the original path
            $request->server->set('REQUEST_URI', 'workshops/'.$workshopTranslation->workshop_id);
            return $request;
        }

        // Fallback continue
        return $request;
    }

}