How to populate checkboxes with data from database?










1















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,
);










share|improve this question






















  • 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















1















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,
);










share|improve this question






















  • 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













1












1








1








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,
);










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 9:16









laravelnewbielaravelnewbie

217




217












  • 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

















  • 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
















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












1 Answer
1






active

oldest

votes


















0














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');






share|improve this answer
























    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    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');






    share|improve this answer





























      0














      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');






      share|improve this answer



























        0












        0








        0







        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');






        share|improve this answer















        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');







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 10:21

























        answered Nov 14 '18 at 10:15









        IndianCodingIndianCoding

        1,044119




        1,044119





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            What does pagestruct do in Eviews?

            Dutch intervention in Lombok and Karangasem

            Channel Islands