laravel-localization
laravel-localization copied to clipboard
Handle field name in route parameter
Right now, the current localization system does not handle using field names in route parameters.
Example
Route definitions
french: /ville/{city:slug}
--> default language
english: /city/{city:slug}
Using LaravelLocalization::getLocalizedURL()
to get the english url, it will return /en/ville/{city:slug}
.
After checking, it seems that when getting the attributes it ignores :slug
and uses the key city
and then tries to find {city} in the translated url to return it.
I wish to not use the route binding on my model as the id and other attributes are used in other places.
Thanks in advance !
Same issue
Is this fixed?
I switched my routes from /page/{model}
to /page/{model:slug}
and /some-other-page/{model:id}
because in some routes I need to fetch same model via slug and in some via ID and package stopped to translate my routes.
Before routes switch I used this fix (which works for my specific use case where all routes that require slug
starts with pages.
), this isn't long-term solution and it would be good to fix this small bug:
public function getRouteKeyName()
{
return request()->route() && str_starts_with(request()->route()->getName(), 'pages.') ? 'slug' : 'id';
}