Tidy up code with Laravel Route Model Binding

Last Updated at : September 30, 2017 By - Usama Muneer | 2288

    Introduction

    Laravel Route Model binding is a great way to speed up your development and is a good way to clean up your code a bit. Laravel Route Model binding provides a convenient way to inject class instances into your routes.

    For example, instead of injecting a user’s ID, you can inject the entire User class instance that matches the given ID.

    Basic Routing

    Most of the routes in your application are located in /routes/web.php file, which are loaded by App\Providers\RouteServiceProvider class. The most basic Laravel Route accepts a URI and a closure.

    <?php
    
    Route::get('/', function(){
    
        return 'Hello world';
    
    });

    Example

    Let’s say we want to visit a User by their IDs, we normally have the following Route setup to achieve this.

    <?php
    
    Route::get('/users/{id}', function($id, App\User $user){
    
        $user = $user->findOrFail($id);
        dd($user->name);
    
    });

    Route Model Binding

    With Laravel Route Model binding, this process simplifies to some extent & its a quite easy way to do thing like these.

    To use Route Model binding, we first need to register the Model inside of the boot() method of RouteServiceProvider class.

    <?php
    public function boot(Router $router)
    {
          parent::boot();
          $router->model('user',User::class);
    }

    Now, we can use the user binding inside of our /routes/web.php file to achieve the similar results:

    <?php
    
    Route::get('/users/{user}', function($user){
      //we can now directly say
       dd($user->name);
    });

    Final Words

    Laravel Route Model binding works well with nested URIs which sometimes quite difficult to tackle with basic routing. So give it a try yourself and try if thats work our for you. If you have any feedback or comments, write down in the comment box below. You can also follow us on Twitter.

    blog user
    Usama Muneer

    A web enthusiastic, self-motivated & detail-oriented professional Full-Stack Web Developer from Karachi, Pakistan with experience in developing applications using JavaScript, WordPress & Laravel specifically. Loves to write on different web technologies with an equally useful skill to make some sense out of it.

    Related Posts

    Importing Excel files come handy when you’re building some large scale inventory kind of an application where you need to import set of data from CSV or excel files. In this short tutorial, we’ll go through on importing excel files with Laravel & look on for some crazy hacks and tricks in between.

    Gear up fellow developers, another Laravel Meetup is coming back to Lahore & it will be a great milestone while helping our fellow web developers across the city to become motivated and will help them grow as Laravel developers.

    Getting an app developed is not the only thing that one needs to ensure in terms of digital visibility and e-commerce. Yes, having an app is important but to stay updated and to be abreast with the competition Optimization of performance is necessary.

    Telescope gives knowledge into the solicitations coming into your application, exemptions, log sections, database questions, lined employments, mail, notices, reserve activities, planned errands, variable dumps and that's just the beginning.