doujun5009 2017-05-18 15:28
浏览 99
已采纳

Laravel从模型中获得多项选择

I have User Role models defined in a many-to-many relationship. When creating user I have a multiple select box to pick roles for specific user:

<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
    <div class="form-group">
        <label for="roles">Roles</label>
        {!! Form::select('roles[]',$roles, null,['class' => 'form-control selectpicker','multiple' => 'true']) !!}
    </div>
</div>

This is a form which is shared in both create and edit methods, but for edit it is like this:

{!! Form::model($patient, ['route' => ['patients.update', $patient->id], 'method' => 'PUT']) !!}
    @include('patients.partials.form')
{!! Form::close()!!}

When I try to edit user with roles, no roles get selected...this is the controller

public function edit(User $user)
{
    $clinics = Clinic::pluck('name', 'id');
    $roles = Role::pluck('display_name', 'id');
    $states = State::pluck('name', 'id');
    $cities = City::pluck('name', 'id');
    return view('users.edit', compact('user', 'clinics', 'states', 'cities', 'roles'));
}

EDIT:

I handle update like this:

public function update(Request $request, User $user)
{
    if ($request->has('password'))
        $user->update($request->except('password_confirmation'));
    else
        $user->update($request->except('password', 'password_confirmation'));

    $user->roles()->sync($request->roles);
    return redirect('users')->with('status', 'User Updated!');
}

Part of the request dump:

"roles": [
    "1",
    "4"
],
"first_name": "xy",
"last_name": "yx",
  • 写回答

1条回答 默认 最新

  • douhezi2285 2017-05-19 04:39
    关注

    The third parameter of the form select takes the selected array of item. Make the following changes for your selected roles to show up when you edit.

    // Pass the current user roles
    public function edit(User $user)
    {
        $userRoles = $user->roles->pluck('id');
        $clinics = Clinic::pluck('name', 'id');
        $roles = Role::pluck('display_name', 'id');
        $states = State::pluck('name', 'id');
        $cities = City::pluck('name', 'id');
        return view('users.edit', compact('user', 'userRoles', 'clinics', 'states', 'cities', 'roles'));
    }
    
    // Pass the selected list of roles 
    {!! Form::model($patient, ['route' => ['patients.update', $patient->id], 'method' => 'PUT']) !!}
        @include('patients.partials.form', ['selected' => $userRoles])
    {!! Form::close()!!}
    
    // Check if a selected value is given and use it. This makes it reusable with create and edit.
    {!! Form::select('roles[]', $roles, isset($selected) ? $selected : null, [
        'class' => 'form-control selectpicker',
        'multiple' => 'true'
    ]) !!}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)