我的数据库中有“课程”表,其中有 JSON 的“slug”列。例如:
| Slug |
|------------------------------------------------------|
| {"az": "slug-in-azerbaijani", "ru": "slug-in-russian"|
| {"az": "another-slug-az", "ru": "another-slug-in-rus"|
现在,当我进入课程详细信息时,我会通过 slug 获取课程详细信息。这是控制器:
public function detailed($slug) {
$locale = app()->getLocale();
$course = Course::where(\DB::raw( "json_extract(slug, '$." . $locale . "')" ), $slug)->firstOrFail();$popularCourses = Course::inRandomOrder()->limit(3)->get();
if(!$course) {
return abort(404);
}
$data = array(
'course' => $course,
'instructor' => $course->instructor
);
// Fetch detailed information about instructor and courses that belongs to him
return view('courses.detailed')->with($data);
}
但是,有一个问题。如果我在课程详细信息中并且 URL 是:
然后我将网站语言更改为另一种语言,它不会翻译 slug。网址变成
它会抛出 404 页面。然而,它必须是
我怎么解决这个问题?这是route.php:
Route::group(
[
'prefix' => LaravelLocalization::setLocale(),
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
],
function()
{
Route::get('/courses', 'CoursesController@index');
Route::get('/courses/{slug}', 'CoursesController@detailed');
Route::get('/courses/category/{slug}', 'CoursesController@getCoursesByCategory');
});