In the next major release of Laravel, you can customize implicit routing model bindings directly in route definitions:

Route::get('/posts/{post:slug}'.function (Post $post{/ /... });Copy the code

Currently, with Laravel 6, the following requirements require you to define a getRouteKeyName() method on the model like this:

<? php class Post extends Model { /** * Get the route keyfor the model.

     *

     * @return string

     */

    public function getRouteKeyName()

    {

        return 'slug'; }}Copy the code

You can still use the getRouteKeyName() method; However, I think it would be smoother to customize it directly in the route.

You may have multiple routes that you want to bind in different ways. For example, a foreground route uses slugs to display posts, and a background route wants to manage posts by ID

Route::get('/posts/{post:slug}'.function (Post $post{/ /... }); // Alternatively you can use the default '{post}' Route::get('/admin/posts/{post:id}/edit'.function (Post $post{/ /... });Copy the code

If you start experimenting with custom implicit routing model bindings, you can install the development version of Laravel

laravel new example --devCopy the code

That’s all about the simple implicit routing model binding for Laravel 7

To learn more, please visit:

Tencent T3-T4 standard boutique PHP architect tutorial directory directory, as long as you finish the guarantee salary rise a step (continue to update)


I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc.