Skip to content

Commit aef01fe

Browse files
committed
Add route model binding info to README
1 parent 13e769c commit aef01fe

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
- [Generate localized signed route URL's](#-generate-signed-route-urls)
2020
- Allow routes to be [cached](#-cache-routes).
2121
- Optionally [translate each segment](#-translate-routes) in your URI's.
22-
- Use the middleware to [automatically set the appropriate app locale](#-use-middleware-to-update-app-locale) (and use route model binding).
22+
- Use the middleware to [automatically set the appropriate app locale](#-use-middleware-to-update-app-locale) (and use [route model binding](#-route-model-binding)).
2323
- **Work with routes without thinking too much about locales.**
2424

2525
## 🔌 Demo App
@@ -394,6 +394,26 @@ $url = route('posts.show', [$post]); // /en/posts/en-slug
394394
$url = route('posts.show', [$post], true, 'nl'); // /nl/posts/nl-slug
395395
```
396396

397+
## 🚴‍ Route Model Binding
398+
399+
If you enable the [middleware](#-use-middleware-to-update-app-locale) included in this package,
400+
you can use [Laravel's route model binding](https://laravel.com/docs/routing#route-model-binding)
401+
to automatically inject models with localized route keys in your controllers.
402+
403+
All you need to do is add a `resolveRouteBinding()` method to your model.
404+
Check [Laravel's documentation](https://laravel.com/docs/routing#route-model-binding)
405+
for alternative ways to enable route model binding.
406+
407+
```php
408+
public function resolveRouteBinding($value)
409+
{
410+
// Perform the logic to find the given slug in the database...
411+
return $this->where('slug->'.app()->getLocale(), $value)->firstOrFail();
412+
}
413+
```
414+
415+
> **TIP:** checkout [spatie/laravel-translatable](https://github.com/spatie/laravel-translatable) for translatable models.
416+
397417
## 🗃 Cache Routes
398418

399419
In production you can safely cache your routes per usual.

0 commit comments

Comments
 (0)