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
Post a Comment