How to populate checkboxes with data from database?
I can't find the way to populate checkboxes with data from database. I've got roles table(id and role name), users table(id, name, surname, etc.) and a pivot table(user_id, role_id). How do I populate them that it shows if user is admin/user or both?
Thank you.
controller
$total_row = $data->count();
$output = "";
if ($total_row > 0)
foreach ($data as $row)
$roleNames = '';
foreach (Role::all() as $roles1)
$roleNames .= $roles1->role != null ? $roles1->role.' '.'<input type="checkbox" class="checkbox" id="checkboxId">'.' ' : '';
// if ($roles1 = id(1))
// return '<input type="checkbox" class="checkbox" id="checkboxId" checked>';
//
$output .= '
<tr>
<td>'.$row->surname.'</td>
<td>'.$row->name.'</td>
<td>'.$row->phone.'</td>
<td>'.$roleNames.'</td>
<td><button type="button" id="rowId" class="remove-button btn btn-danger" data-id="'.$row->id.'">
<div class="close">x</div>
</button></td>
</tr>
';
else
$output = '
<tr>
<td align="center" colspan="5">Nema podataka</td>
</tr>
';
return array(
'table_data' => $output,
'total_data' => $total_row,
);
php mysql database laravel
add a comment |
I can't find the way to populate checkboxes with data from database. I've got roles table(id and role name), users table(id, name, surname, etc.) and a pivot table(user_id, role_id). How do I populate them that it shows if user is admin/user or both?
Thank you.
controller
$total_row = $data->count();
$output = "";
if ($total_row > 0)
foreach ($data as $row)
$roleNames = '';
foreach (Role::all() as $roles1)
$roleNames .= $roles1->role != null ? $roles1->role.' '.'<input type="checkbox" class="checkbox" id="checkboxId">'.' ' : '';
// if ($roles1 = id(1))
// return '<input type="checkbox" class="checkbox" id="checkboxId" checked>';
//
$output .= '
<tr>
<td>'.$row->surname.'</td>
<td>'.$row->name.'</td>
<td>'.$row->phone.'</td>
<td>'.$roleNames.'</td>
<td><button type="button" id="rowId" class="remove-button btn btn-danger" data-id="'.$row->id.'">
<div class="close">x</div>
</button></td>
</tr>
';
else
$output = '
<tr>
<td align="center" colspan="5">Nema podataka</td>
</tr>
';
return array(
'table_data' => $output,
'total_data' => $total_row,
);
php mysql database laravel
Can you share output ofRole::all()
– dexter
Nov 14 '18 at 9:43
According to me creating checkbox in controller isn't a really good choice. Try creating the checkbox in view . If you are passing this output as an Ajax response, try using jQuery to create dynamic elements.
– dexter
Nov 14 '18 at 9:46
add a comment |
I can't find the way to populate checkboxes with data from database. I've got roles table(id and role name), users table(id, name, surname, etc.) and a pivot table(user_id, role_id). How do I populate them that it shows if user is admin/user or both?
Thank you.
controller
$total_row = $data->count();
$output = "";
if ($total_row > 0)
foreach ($data as $row)
$roleNames = '';
foreach (Role::all() as $roles1)
$roleNames .= $roles1->role != null ? $roles1->role.' '.'<input type="checkbox" class="checkbox" id="checkboxId">'.' ' : '';
// if ($roles1 = id(1))
// return '<input type="checkbox" class="checkbox" id="checkboxId" checked>';
//
$output .= '
<tr>
<td>'.$row->surname.'</td>
<td>'.$row->name.'</td>
<td>'.$row->phone.'</td>
<td>'.$roleNames.'</td>
<td><button type="button" id="rowId" class="remove-button btn btn-danger" data-id="'.$row->id.'">
<div class="close">x</div>
</button></td>
</tr>
';
else
$output = '
<tr>
<td align="center" colspan="5">Nema podataka</td>
</tr>
';
return array(
'table_data' => $output,
'total_data' => $total_row,
);
php mysql database laravel
I can't find the way to populate checkboxes with data from database. I've got roles table(id and role name), users table(id, name, surname, etc.) and a pivot table(user_id, role_id). How do I populate them that it shows if user is admin/user or both?
Thank you.
controller
$total_row = $data->count();
$output = "";
if ($total_row > 0)
foreach ($data as $row)
$roleNames = '';
foreach (Role::all() as $roles1)
$roleNames .= $roles1->role != null ? $roles1->role.' '.'<input type="checkbox" class="checkbox" id="checkboxId">'.' ' : '';
// if ($roles1 = id(1))
// return '<input type="checkbox" class="checkbox" id="checkboxId" checked>';
//
$output .= '
<tr>
<td>'.$row->surname.'</td>
<td>'.$row->name.'</td>
<td>'.$row->phone.'</td>
<td>'.$roleNames.'</td>
<td><button type="button" id="rowId" class="remove-button btn btn-danger" data-id="'.$row->id.'">
<div class="close">x</div>
</button></td>
</tr>
';
else
$output = '
<tr>
<td align="center" colspan="5">Nema podataka</td>
</tr>
';
return array(
'table_data' => $output,
'total_data' => $total_row,
);
php mysql database laravel
php mysql database laravel
asked Nov 14 '18 at 9:16
laravelnewbielaravelnewbie
217
217
Can you share output ofRole::all()
– dexter
Nov 14 '18 at 9:43
According to me creating checkbox in controller isn't a really good choice. Try creating the checkbox in view . If you are passing this output as an Ajax response, try using jQuery to create dynamic elements.
– dexter
Nov 14 '18 at 9:46
add a comment |
Can you share output ofRole::all()
– dexter
Nov 14 '18 at 9:43
According to me creating checkbox in controller isn't a really good choice. Try creating the checkbox in view . If you are passing this output as an Ajax response, try using jQuery to create dynamic elements.
– dexter
Nov 14 '18 at 9:46
Can you share output of
Role::all()– dexter
Nov 14 '18 at 9:43
Can you share output of
Role::all()– dexter
Nov 14 '18 at 9:43
According to me creating checkbox in controller isn't a really good choice. Try creating the checkbox in view . If you are passing this output as an Ajax response, try using jQuery to create dynamic elements.
– dexter
Nov 14 '18 at 9:46
According to me creating checkbox in controller isn't a really good choice. Try creating the checkbox in view . If you are passing this output as an Ajax response, try using jQuery to create dynamic elements.
– dexter
Nov 14 '18 at 9:46
add a comment |
1 Answer
1
active
oldest
votes
Better to use View.
In your case you can try something like this:
$roles = Role::all();
foreach ($data as $row)
$roleNames = '';
$rowRoles = $row->roles->pluck('id');
foreach($roles as $role)
$roleNames .= '<input type="checkbox" name="roles" value="' . $role->id . '" ' . (in_array($role->id, $rowRoles) ? 'checked' : '') . '/> ' . $role->name;
You need to have belongsToMany relation "roles" in your User model.
public function roles()
return $this->belongsToMany('AppRole', 'pivot_table_name');
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296629%2fhow-to-populate-checkboxes-with-data-from-database%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Better to use View.
In your case you can try something like this:
$roles = Role::all();
foreach ($data as $row)
$roleNames = '';
$rowRoles = $row->roles->pluck('id');
foreach($roles as $role)
$roleNames .= '<input type="checkbox" name="roles" value="' . $role->id . '" ' . (in_array($role->id, $rowRoles) ? 'checked' : '') . '/> ' . $role->name;
You need to have belongsToMany relation "roles" in your User model.
public function roles()
return $this->belongsToMany('AppRole', 'pivot_table_name');
add a comment |
Better to use View.
In your case you can try something like this:
$roles = Role::all();
foreach ($data as $row)
$roleNames = '';
$rowRoles = $row->roles->pluck('id');
foreach($roles as $role)
$roleNames .= '<input type="checkbox" name="roles" value="' . $role->id . '" ' . (in_array($role->id, $rowRoles) ? 'checked' : '') . '/> ' . $role->name;
You need to have belongsToMany relation "roles" in your User model.
public function roles()
return $this->belongsToMany('AppRole', 'pivot_table_name');
add a comment |
Better to use View.
In your case you can try something like this:
$roles = Role::all();
foreach ($data as $row)
$roleNames = '';
$rowRoles = $row->roles->pluck('id');
foreach($roles as $role)
$roleNames .= '<input type="checkbox" name="roles" value="' . $role->id . '" ' . (in_array($role->id, $rowRoles) ? 'checked' : '') . '/> ' . $role->name;
You need to have belongsToMany relation "roles" in your User model.
public function roles()
return $this->belongsToMany('AppRole', 'pivot_table_name');
Better to use View.
In your case you can try something like this:
$roles = Role::all();
foreach ($data as $row)
$roleNames = '';
$rowRoles = $row->roles->pluck('id');
foreach($roles as $role)
$roleNames .= '<input type="checkbox" name="roles" value="' . $role->id . '" ' . (in_array($role->id, $rowRoles) ? 'checked' : '') . '/> ' . $role->name;
You need to have belongsToMany relation "roles" in your User model.
public function roles()
return $this->belongsToMany('AppRole', 'pivot_table_name');
edited Nov 14 '18 at 10:21
answered Nov 14 '18 at 10:15
IndianCodingIndianCoding
1,044119
1,044119
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296629%2fhow-to-populate-checkboxes-with-data-from-database%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you share output of
Role::all()– dexter
Nov 14 '18 at 9:43
According to me creating checkbox in controller isn't a really good choice. Try creating the checkbox in view . If you are passing this output as an Ajax response, try using jQuery to create dynamic elements.
– dexter
Nov 14 '18 at 9:46