laravel 模型绑定

namespace AppProviders;

class RouteServiceProvider extends ServiceProvider
{

    public function boot(Router $router)
    {

        parent::boot($router);
         
        //在这里添加绑定
        $route->model('articles', 'AppArticle');   
   //或者
$route->bind('articles', function($id){
//do somthing...
});
}

参数articles用php artisan route:list 可以查到 articles{articles},这样在控制器中直接得到的是对应的article了,不用自己去通过ID查询了。

原文地址:https://www.cnblogs.com/fenle/p/4805997.html