php - Laravel 5 Route::group with public variable -


i have code this:

route::group(['prefix'=>'dashboard'],function(){             route::get('addnew',function(){         $user = db::table('users')->where('username','=',session('username'))->first();         $data = array('level' => $user->level, 'name' => $user->name,'email' => $user->email);         return view('layout.addnew')->with($data);     });     route::get('load',function(){         $user = db::table('users')->where('username','=',session('username'))->first();         $data = array('level' => $user->level, 'name' => $user->name,'email' => $user->email);         return view('layout.load')->with($data);     }); }); 

but don't work when use public variable this:

route::group(['prefix'=>'dashboard'],function(){      $user = db::table('users')->where('username','=',session('username'))->first();     $data = array('level' => $user->level, 'name' => $user->name,'email' => $user->email);      route::get('addnew',function(){                 return view('layout.addnew')->with($data);     });     route::get('load',function(){                 return view('layout.load')->with($data);     }); }); 

help me please!

as you're using closure, need explicitly grant access variables, outside of context of function. "use" function.

route::group(['prefix'=>'dashboard'],function(){      $user = db::table('users')->where('username','=',session('username'))->first();     $data = array('level' => $user->level, 'name' => $user->name,'email' => $user->email);      route::get('addnew',function() use($data) {                 return view('layout.addnew')->with($data);     });     route::get('load',function() use($data) {                 return view('layout.load')->with($data);     }); }); 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -