laravelnewbie
laravelnewbie

Reputation: 41

Laravel / Populate checkbox

how do I populate each checkbox with its id? Stupid thing to get stuck on but anything would help. Currently it's populating every checkbox.

$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>
                ';
            }

EDIT: $userRoles is printing out JSON with all users and its roles

[{"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}}]

Upvotes: 1

Views: 71

Answers (2)

Mehdi Atraimche
Mehdi Atraimche

Reputation: 1

Try to check if your user roles collection object contains the courant role object

Replace this line

if (true) {

By this

if($userRoles->contains('id', $roles1->id))

Upvotes: 0

Oleksandr Pobuta
Oleksandr Pobuta

Reputation: 405

can you try to make this you should compare your roles with those what user has, not setting true in a loop

 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">'.' ' : '';
 }

Upvotes: 1

Related Questions