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.

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

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加