Last active
July 1, 2020 05:21
-
-
Save mkwsra/8713985836d6d8c606821fdbbf09034e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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