Saturday, August 13, 2016

Laravel 5.2 Implicit model binding

In Laravel 5.2 new feature called implicit model binding added. Which can be implement efficiently and in a very easy manner. Here how its done :
  1. In your routes.php file create a new route
    Route::get('user',function(){});
  2. Now if you want to retrieve information of a particular user, modify your user route as follow
    Route::get('user/{user}',function(App\User $user){ return $user; 
    });
Now if you browse your defined route , localhost:8000/user/1
(Assuming your laravel application running on localhost, port 8000 and you have data on your user table)
This will return information in JSON format of the user who has id 1.
Here , App\User is the namespace for User model.

0 comments:

Post a Comment