Laravel 7 will be out 2020 with new awesome features. You can clean the way routing is been done with one line of code.
But you can try the dev version by running this code
laravel new example --dev
In laravel 7 you can bind routes like this:
Route::get('/posts/{post:slug}', function (Post $post) { // ... });
Currently, Laravel 6 and below requires you to define a getRouteKeyName()
method on the model like so:
<?php class Post extends Model { /** * Get the route key for the model. * * @return string */ public function getRouteKeyName() { return 'slug'; } }
You can use the below route binding when you have multiple routes that you want to bind differently.
For example, the frontend route uses slugs to display posts and backend admin uses ids to manage posts:
Route::get('/posts/{post:slug}', function (Post $post) { // ... }); // Or you could use the default `{post}` here... Route::get('/admin/posts/{post:id}/edit', function (Post $post) { // ... });
Thanks for reading.
Continue Reading
Timmortal
Thanks for this amazing tutorial, kizhinho.
There are so much i can take away from this and refractor some PHP that i have been working on, hope to see more from you soon.