Laravel 5 : passing a Model parameter to the middleware -


i pass model parameter middleware. according link (laravel 5 middleware parameters) , can include parameter in handle() function :

 public function handle($request, closure $next, $model)  {    //perform actions  } 

how pass in constructor of controller? isn't working :

public function __construct(){     $model = new model();     $this->middleware('mycustommw', $model); } 

**note : ** important pass different models (ex. modelx, modely, modelz)

first of make sure you're using laravel 5.1. middleware parameters weren't available in prior versions.

now don't believe can pass instantiated object parameter middleware, (if need this) can pass model's class name , i.e. primary key if need specific instance.

in middleware:

public function handle($request, closure $next, $model, $id) {     // instantiate model off of ioc , find specific 1 id     $model = app($model)->find($id);     // whatever need model      return $next($request); } 

in controller:

use app\user;  public function __construct() {     $id = 1;      // use middleware , pass model's class name , id     $this->middleware('mycustommw:'.user::class.",$id");  } 

with approach can pass whatever models want middleware.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -