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;
}
}