Illuminate\Routing\Router::model PHP Method

model() public method

Register a model binder for a wildcard.
public model ( string $key, string $class, Closure $callback = null ) : void
$key string
$class string
$callback Closure
return void
    public function model($key, $class, Closure $callback = null)
    {
        $this->bind($key, function ($value) use($class, $callback) {
            if (is_null($value)) {
                return;
            }
            // For model binders, we will attempt to retrieve the models using the first
            // method on the model instance. If we cannot retrieve the models we'll
            // throw a not found exception otherwise we will return the instance.
            $instance = $this->container->make($class);
            if ($model = $instance->where($instance->getRouteKeyName(), $value)->first()) {
                return $model;
            }
            // If a callback was supplied to the method we will call that to determine
            // what we should do when the model is not found. This just gives these
            // developer a little greater flexibility to decide what will happen.
            if ($callback instanceof Closure) {
                return call_user_func($callback, $value);
            }
            throw (new ModelNotFoundException())->setModel($class);
        });
    }

Usage Example

 /**
  * Boot the application events.
  *
  * @return void
  */
 public function boot(Router $router)
 {
     $router->model('uuser', '\\Modules\\Users\\Entities\\User');
     $router->model('urole', '\\Bican\\Roles\\Models\\Role');
     $this->registerTranslations();
     $this->registerConfig();
     $this->registerViews();
     Relation::morphMap(['users' => \Modules\Users\Entities\User::class]);
 }
All Usage Examples Of Illuminate\Routing\Router::model