Laravel passing username parameter to controller from user.blade.php -
hi on website have profile username parameter root/user/{username}. planning add button block user. problem when other user click block button, button stuffs in check.php controller doesn't pass user/{username} parameter need in if statements. question how can pass {username} parameter user.blade.php check.php controller?
as not seeing code here, seems need, have create route pointing controller method , route redirect users when successufully blocked:
route::get('user/block/{username}', 'blockusercontroller@block'); route::get('userblocked', 'blockusercontroller@blocked');
and controller itself:
class blockusercontroller extends controller { public function block($username) { $user = user::where('username', $username); $user->blocked = true; $user->save(); return redirect::to('userblocked'); } public function blocked($username) { return view::make('user.blocked'); } }
and if click button pointing route:
http://application.com/user/block/user3398940
it blocked.
if want go little more advanced in laravel, can use dependency injection , remove code controller:
class blockusercontroller extends controller { private $user; public function __construct(user $user) $this->user = $user; } public function block($username) { if ($user->block($username)) { return redirect::to('userblocked'); } return redirect::back()->with('message', 'user not found'); } public function blocked($username) { return view::make('user.blocked'); } }
and user model have have block method:
class user extends eloquent { public function block($username) { if ($user = $this->newquery()->where('username', $username)) { $user->blocked = true; return $user->save(); } return false; } }
Comments
Post a Comment