【问题标题】:Laravel / Populate checkboxLaravel / 填充复选框
【发布时间】:2019-04-18 08:31:23
【问题描述】:

如何使用其 ID 填充每个复选框?愚蠢的事情被卡住了,但任何事情都会有所帮助。目前它正在填充每个复选框。

$total_row = $data->count();
        $output = "";
        if ($total_row > 0) {
            foreach ($data as $row) {
                $roleNames = '';
                $userRoles = $row->roles;
                $checked = '';
                foreach (Role::all() as $roles1) {
                    if (true) {
                        $checked = 'checked="checked"';
                    }
                    $roleNames .= $roles1->role != null ? $roles1->role.' '.'<input type="checkbox" '.$checked.' name="role" value="'.$roles1->id.'" class="checkbox" id="checkboxId">'.' ' : '';
                }
                $output .= '
                    <tr>
                        <td>'.$row->surname.'</td>
                        <td>'.$row->name.'</td>
                        <td>'.$row->phone.'</td>
                        <td>'.$roleNames.'</td>
                        <td>'.$userRoles.'</td>
                        <td><button type="button" id="rowId" class="remove-button btn btn-danger" data-id="'.$row->id.'">
                        <div class="close">&#120;</div>
                        </button></td>
                    </tr>
                ';
            }

编辑: $userRoles 正在打印所有用户及其角色的 JSON

[{"id":1,"role":"Admin","created_at":"2018-11-12 10:52:40","updated_at":"2018-11-12 10:52:40","pivot":{"user_id":1,"role_id":1}}]
[{"id":2,"role":"Korisnik","created_at":"2018-11-12 10:52:40","updated_at":"2018-11-12 10:52:40","pivot":{"user_id":2,"role_id":2}}]
[{"id":2,"role":"Korisnik","created_at":"2018-11-12 10:52:40","updated_at":"2018-11-12 10:52:40","pivot":{"user_id":3,"role_id":2}}]
[{"id":2,"role":"Korisnik","created_at":"2018-11-12 10:52:40","updated_at":"2018-11-12 10:52:40","pivot":{"user_id":4,"role_id":2}}]
[{"id":1,"role":"Admin","created_at":"2018-11-12 10:52:40","updated_at":"2018-11-12 10:52:40","pivot":{"user_id":9,"role_id":1}},{"id":2,"role":"Korisnik","created_at":"2018-11-12 10:52:40","updated_at":"2018-11-12 10:52:40","pivot":{"user_id":9,"role_id":2}}]
[{"id":2,"role":"Korisnik","created_at":"2018-11-12 10:52:40","updated_at":"2018-11-12 10:52:40","pivot":{"user_id":10,"role_id":2}}]

【问题讨论】:

    标签: php laravel if-statement roles


    【解决方案1】:

    你可以尝试让你应该将你的角色与用户拥有的角色进行比较,而不是在循环中设置 true

     foreach (Role::all() as $roles1) {
         if (in_array($roles1->role, (array)$userRoles)) {
            $checked = 'checked="checked"';
         }
         $roleNames .= $roles1->role != null ? $roles1->role.' '.'<input type="checkbox" '.$checked.' name="role" value="'.$roles1->id.'" class="checkbox" id="checkboxId">'.' ' : '';
     }
    

    【讨论】:

    • 不做这项工作,不填充任何东西
    • 你能把你的 $roles1$userRoles 转储到问题中吗?
    • in_array() 期望参数 2 是数组,给定对象
    • @laravelnewbie 再试一次)
    • 仍然没有填充复选框... :(
    【解决方案2】:

    尝试检查您的用户角色集合对象是否包含 courant 角色对象

    替换此行

    if (true) {

    由此

    if($userRoles-&gt;contains('id', $roles1-&gt;id))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-15
      • 2015-01-08
      • 1970-01-01
      • 2014-11-08
      相关资源
      最近更新 更多