douxihui8270 2014-05-02 17:27
浏览 64
已采纳

Laravel 4 - 使用多个复选框重新填充表单进行编辑

I'm having a hard time figuring out how to repopulate a form for edit that has check boxes in it. I think the most confusing part is because they are coming from a pivot table.

I have users, permissions, and users_permissions tables.

For a quick demonstration of what the tables look like, I ran this query and included a screen clip of the results.

return $userPermissions = User::with('permissions')->find($id);

Output of users table with permissions

In my form I just have two permissions check boxes for now until I get the concept working, then I will add a foreach loop and grab them all from the database, but now I have the following:

<div class="form-group">
    <label>
        {{ Form::hidden('permissions[4]', '0', ['class' => 'checkbox-inline']) }}
        {{ Form::checkbox('permissions[4]', '1', ['class' => 'checkbox-inline']) }}
        Manage Content
     </label>
 </div>
 <div class="form-group">
     <label>
         {{ Form::hidden('permissions[3]', '0', ['class' => 'checkbox-inline']) }}
         {{ Form::checkbox('permissions[3]', '1', ['class' => 'checkbox-inline']) }}
         Manage Users
     </label>
  </div>

I'm not sure if this is useful information, but when I first create the user, I create a permissions array to attach to the new user. Here is the code for that,

public function createUserPermissionsArray($input)
{
    $permissionsArray = [];
    $permissions = $input['permissions'];

    foreach ($permissions as $id => $value)
    {
        if ($value == 1)
        {
            array_push($permissionsArray, $id);
        }
    }
    $this->saveNewUserToDatabase($input, $permissionsArray);

}

I really need some direction here about how to solve this problem. Thanks

  • 写回答

1条回答 默认 最新

  • dongyan1808 2014-05-02 17:50
    关注

    I recently worked on something similar, except I had groups with permissions assigned to them, which users were then assigned to. For example, for GroupController::edit() I had this code:

    public function edit($id)
    {
        if(!permitted('group.edit')) {
            return Redirect::route('user.dashboard');
        }
    
        $group = Group::find($id);
        $permissions = Permission::all();
    
        return View::make('admin.group.edit', [
            'group' => $group, 
            'permissions' => $permissions, 
            'assigned' => $group->permissions->lists('id')
        ]);
    }
    

    Then within the form partial for the view (admin.group.partials.form), I had the following code to handle permissions:

    @foreach($permissions as $permission)
        <div class="row">
            @if(isset($assigned))
                {{ Form::checkbox(
                    'permissions[' . $permission->ident .']', 
                    $permission->id, 
                    in_array($permission->id, $assigned)) 
                }}<label for="permissions">{{ $permission->ident }} - {{ $permission->description }}</label>
            @else
                {{ Form::checkbox(
                    'permissions[' . $permission->ident .']', 
                    $permission->id) 
                }}<label for="group">{{ $permission->ident }} - {{ $permission->description }}</label>
            @endif
        </div>
    @endforeach
    

    I've formatted it the best I can, so that it's easy to read. Basically I had a create.blade.php and edit.blade.php which both include the form partial, but each file handles the opening and closing of the form (model binding for edit, normal open for create), hence the if statement.

    Unfortunately I couldn't find a better method to achieve this. I hope this helps you.

    P.S: As a side note, I've actually written a tutorial about creating a robust and simple ACL with Laravel, I can link if you'd like.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法