按路由传递用户信息

时间:2015-11-16 08:26:40

标签: laravel authentication blade middleware

我有一个刀片文件有用户信息

{{$user -> name}}
{{$user -> age}}
{{$user -> gender}}

我还有一个表单来添加此用户的权限

{!! Form::open(['method' => 'post'])!!}
<div>
<label >
<input type="checkbox" value="1"> Setting
</label>
<label >
<input type="checkbox "value="1"> Users
</label>
<label>
<input type="checkbox"  value="1"> Images
</label>
</div>
<br>
{!! Form::submit('Save')!!}
{!! Form::close()!!}

我用这样的路由发送用户的id

Route::post('roles/{$user-> name}','control@save_roles');

保存此功能

   public function save_roles(Request $request,$id)
{
    $role = new Role;
    $role -> user_id =  $user -> name;
    if($request ->has('setting')){$role ->setting = 1;}
    if($request ->has('users')){$role ->users = 1;}
    if($request ->has('images')){$role ->images = 1;}
    $role -> save();
    return redirct('users');
}

但没有工作

3 个答案:

答案 0 :(得分:1)

我认为你的路线应该是

Route::post('roles/{$userId}','control@save_roles');

和您的控制器功能

public function save_roles(Request $request, $userId)
{
    $role = new Role;
    $role->user_id =  $userId;
.
.
.
}

答案 1 :(得分:0)

你在这里做错了:

string cStr= Request.QueryString["c"];
string nStr= Request.QueryString["n"];

答案 2 :(得分:0)

好的,您有几个问题需要修复。

首先,让我们修复你的路线:

Route::post('roles/{username}','control@save_roles');

然后您的表单需要一个操作来将信息发布到:

{!! Form::open(['method' => 'post','action'=>['control@save_roles',$user->name]])!!}
<div>
<label >
<input type="checkbox" value="1"> Setting
</label>
<label >
<input type="checkbox "value="1"> Users
</label>
<label>
<input type="checkbox"  value="1"> Images
</label>
</div>
<br>
{!! Form::submit('Save')!!}
{!! Form::close()!!}

最后,在您的Controller操作中:

public function save_roles(Request $request,$username)
{
    $role = new Role;
    $role -> user_id =  $username;
    if($request ->has('setting')){$role ->setting = 1;}
    if($request ->has('users')){$role ->users = 1;}
    if($request ->has('images')){$role ->images = 1;}
    $role -> save();
    return redirect('users');
}