如何通过ID检索数组数据到Laravel 9中的多个复选框

发布于 2025-02-05 05:20:47 字数 2074 浏览 0 评论 0原文

这是我的控制器函数:

public function get_edit_user ($id) {

    $user = User::where('id', $id)->first();

    return view('adminpanel.user.edituser', compact('user'));

}

这是我的刀片视图:

  <div class="col-md-6">
          <div class="form-group row">
              <label class="col-sm-2 col-form-label">Roles</label>
            <div class="col-sm-4">
              <div class="form-check">
                  <label class="form-check-label">

                    <input type="checkbox" class="form-check-input" name="role[]"
                    value="{{$user->id}}" {{ $user->role == ["SuperAdmin"] ? 'checked' : '' }}  >
                 Super Admin
                  </label>
                </div>
              </div>
   

               <div class="col-sm-3">
                   <div class="form-check">
                       <label class="form-check-label">
                         <input type="checkbox" class="form-check-input" name="role[]"  
                        value="{{$user->id}}" {{ $user->role == ["Admin"] ? 'checked' : '' }} >
                   Admin
                       </label>
                     </div>
               </div>

               <div class="col-sm-3">
                   <div class="form-check">
                       <label class="form-check-label">
                         <input type="checkbox" class="form-check-input" name="role[]" 
                         value="{{$user->id}}" {{ $user->role == ["User"] ? 'checked' : '' }}>
                        User
                       </label>
                     </div>
               </div>


           </div>
         </div>
                        
     </div>

如果用户有一个角色,例如超级助手可以检索它。但是,当用户具有诸如SuperAdmin和用户之类的多个角色时,它不会检索任何一个。我试图将其检索到复选框。

数据库内的数据存储为数组EX:[“ SuperAdmin”,“ Admin”,“用户”]。我可以轻松存储数据,但是当您尝试通过ID检索它时,我会遇到此问题。 谢谢

This is my Controller function:

public function get_edit_user ($id) {

    $user = User::where('id', $id)->first();

    return view('adminpanel.user.edituser', compact('user'));

}

And this is my Blade view:

  <div class="col-md-6">
          <div class="form-group row">
              <label class="col-sm-2 col-form-label">Roles</label>
            <div class="col-sm-4">
              <div class="form-check">
                  <label class="form-check-label">

                    <input type="checkbox" class="form-check-input" name="role[]"
                    value="{{$user->id}}" {{ $user->role == ["SuperAdmin"] ? 'checked' : '' }}  >
                 Super Admin
                  </label>
                </div>
              </div>
   

               <div class="col-sm-3">
                   <div class="form-check">
                       <label class="form-check-label">
                         <input type="checkbox" class="form-check-input" name="role[]"  
                        value="{{$user->id}}" {{ $user->role == ["Admin"] ? 'checked' : '' }} >
                   Admin
                       </label>
                     </div>
               </div>

               <div class="col-sm-3">
                   <div class="form-check">
                       <label class="form-check-label">
                         <input type="checkbox" class="form-check-input" name="role[]" 
                         value="{{$user->id}}" {{ $user->role == ["User"] ? 'checked' : '' }}>
                        User
                       </label>
                     </div>
               </div>


           </div>
         </div>
                        
     </div>

if the user has one role for example SuperAdmin it can retrieve it. But when user has multiple role like SuperAdmin and User it does not retrive any of it.I am trying to retrieve them to checkboxes.

The data storing inside database as array ex: ["SuperAdmin","Admin","User"]. I can store data easyly but when you try to retrieve it by id i got this issue.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若言繁花未落 2025-02-12 05:20:48

通过您的问题,我猜您在以数组格式存储的数据库中获取数据(或看起来像一个)。您的病情应该是:

  in_array("SuperAdmin",  $user->role) ? 'checked' : ''

使用它:

<input type="checkbox" class="form-check-input" name="role[]"
                    value="{{$user->id}}" {{ in_array("SuperAdmin", $array) ? 'checked' : '' }}  >

By your question, I am guessing you are having trouble getting data from the database that is stored in an array format (or what looks like one). your condition should be:

  in_array("SuperAdmin",  $user->role) ? 'checked' : ''

Use it like:

<input type="checkbox" class="form-check-input" name="role[]"
                    value="{{$user->id}}" {{ in_array("SuperAdmin", $array) ? 'checked' : '' }}  >
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文