Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mkwsra/8713985836d6d8c606821fdbbf09034e to your computer and use it in GitHub Desktop.
Save mkwsra/8713985836d6d8c606821fdbbf09034e to your computer and use it in GitHub Desktop.
<?php
namespace App\Providers;
// Example models.
use App\Models\Post;
use App\Models\Taxonomy;
use App\Models\University;
use App\Models\LanguageInstitute;
use App\Models\Exam;
// ...
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
class TranslatableServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
// Add your own models and route variables here correspondingly.
// the followings array items are examples.
$routeModelBindingsUsingUUIDs = [
'post' => Post::class,
'page' => Post::class,
'taxonomy' => Taxonomy::class,
'university' => University::class,
'languageInstitute' => LanguageInstitute::class,
'exam' => Exam::class,
];
foreach ($routeModelBindingsUsingUUIDs as $routeVariable => $class) {
Route::bind($routeVariable, function ($uuid) use ($class) {
if (strpos($uuid, '-') >= 0) {
$uuidExploded = explode('-', $uuid);
$uuid = $uuidExploded[count($uuidExploded) - 1];
}
return $class::where('uuid', $uuid)->firstOrFail();
});
}
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment